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 fs = require('fs');
const path = require('path'); 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 * Load all request handlers in the given folder
* *
* @param {any} app express app * @param {any} app express app
* @param {string} modulefolder folder that contains all modules * @param {string} modulefolder
* @param {options} options * @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 ( module.exports = function (app, modulefolder, opts = {}, subdir = '') {
app,
modulefolder,
options = { opts: undefined, subdir: '', verbose: false }
) {
const { opts, subdir, verbose } = options;
for (const f of fs.readdirSync(modulefolder)) { for (const f of fs.readdirSync(modulefolder)) {
const regex = /(.*?)-(.*?)\.js/; const regex = /(.*?)-(.*?)\.js/;
let [, method, url] = regex.exec(f); let [, method, url] = regex.exec(f);
@ -36,8 +24,6 @@ module.exports = function (
handler(req, res, next, opts); handler(req, res, next, opts);
}; };
if (verbose) console.log(`[requestor info] redirecting ${url} to ${f}`);
switch (method) { switch (method) {
case 'post': case 'post':
app.post(url, func); app.post(url, func);
@ -54,11 +40,6 @@ module.exports = function (
case 'all': case 'all':
app.all(url, func); app.all(url, func);
break; 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', () => { it('should build requests with subdirectory', () => {
requestor(mock, './test/sub', { subdir: 'test' }); requestor(mock, './test/sub', {}, 'test');
expect(mock.registered).to.have.all.keys([ expect(mock.registered).to.have.all.keys([
'post-/test/sub/', 'post-/test/sub/',
'get-/test/sub/', 'get-/test/sub/',