diff --git a/index.js b/index.js index 7cddfca..6b722ab 100644 --- a/index.js +++ b/index.js @@ -5,11 +5,14 @@ const path = require('path'); * Load all request handlers in the given folder * * @param {any} app express app - * @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 /...) + * @param {string} modulefolder folder that contains all modules + * @param {{opts: any, subdir: string, verbose: boolean}} options */ -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)) { const regex = /(.*?)-(.*?)\.js/; let [, method, url] = regex.exec(f); @@ -24,6 +27,8 @@ module.exports = function (app, modulefolder, opts = {}, subdir = '') { handler(req, res, next, opts); }; + if (verbose) console.log(`[requestor info] redirecting ${url} to ${f}`); + switch (method) { case 'post': app.post(url, func);