Compare commits

..

No commits in common. "a73e0d1d8081aed09a1c99a52c91cb2f2a9026b3" and "b2d9e9f0bf9b7452587e91acd9b3f55f4b13f5b9" have entirely different histories.

2 changed files with 5 additions and 24 deletions

View File

@ -1,27 +1,15 @@
const fs = require('fs');
const path = require('path');
/**
* @typedef {Object} options
* @property {any} [opts] object to pass to the handlers
* @property {string} [subdir] subdirectory for all requests
* @property {boolean} [verbose] enable verbose logging
*/
/**
* Load all request handlers in the given folder
*
* @param {any} app express app
* @param {string} modulefolder folder that contains all modules
* @param {options} options
* @param {string} modulefolder
* @param {any} opts object to pass to the handlers (for example database access)
* @param {string} subdir subdirectory for all requests (app will respond to /subdir/... instead of /...)
*/
module.exports = function (
app,
modulefolder,
options = { opts: undefined, subdir: '', verbose: false }
) {
const { opts, subdir, verbose } = options;
module.exports = function (app, modulefolder, opts = {}, subdir = '') {
for (const f of fs.readdirSync(modulefolder)) {
const regex = /(.*?)-(.*?)\.js/;
let [, method, url] = regex.exec(f);
@ -36,8 +24,6 @@ module.exports = function (
handler(req, res, next, opts);
};
if (verbose) console.log(`[requestor info] redirecting ${url} to ${f}`);
switch (method) {
case 'post':
app.post(url, func);
@ -54,11 +40,6 @@ module.exports = function (
case 'all':
app.all(url, func);
break;
default:
if (verbose) {
console.warn(`'${method}' did not match any request method, ignoring`);
}
break;
}
}
};

View File

@ -63,7 +63,7 @@ describe('requestor', () => {
});
it('should build requests with subdirectory', () => {
requestor(mock, './test/sub', { subdir: 'test' });
requestor(mock, './test/sub', {}, 'test');
expect(mock.registered).to.have.all.keys([
'post-/test/sub/',
'get-/test/sub/',