add verbose logging, optional args structured as options

This commit is contained in:
Timo Hocker 2019-12-11 12:55:30 +01:00
parent b2d9e9f0bf
commit 0c4ccc3b43

View File

@ -5,11 +5,14 @@ const path = require('path');
* 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 * @param {string} modulefolder folder that contains all modules
* @param {any} opts object to pass to the handlers (for example database access) * @param {{opts: any, subdir: string, verbose: boolean}} options
* @param {string} subdir subdirectory for all requests (app will respond to /subdir/... instead of /...)
*/ */
module.exports = function (app, modulefolder, opts = {}, subdir = '') { module.exports = function (app, modulefolder, options) {
let { opts, subdir, verbose } = options;
if (typeof subdir === 'undefined') subdir = '';
if (typeof verbose === 'undefined') verbose = false;
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);
@ -24,6 +27,8 @@ module.exports = function (app, modulefolder, opts = {}, subdir = '') {
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);