diff --git a/README.md b/README.md index fb019cf..af5091a 100644 --- a/README.md +++ b/README.md @@ -1 +1,60 @@ # Requestor + +Split express request handlers into individual files to make your api a little +more structured. + +## Installation + +npmrc: + +used to set the registry for @scode + +```npmrc +@scode:registry=https://npm.scode.ovh +``` + +install the package: + +`npm i --save @scode/requestor` + +## Usage + +The path to which the individual modules respond is defined by their name. + +'.' is used as path separator. + +`method-subdir.subdir.subdir.js` + +method can be any of [post, get, put, delete, all] + +to avoid names like `get-.js` root is used for the root directory +(`get-root.js`) + +index.js: + +```js +const requestor = require('requestor'); +const express = require('express'); + +const app = express(); +requestor(app, 'api', {foo: 'bar'}); +app.listen(3000); +``` + +api/get-root.js + +```js +// will respont to get requests on / +module.exports = (req, res, next, opts) => { + res.send(opts.foo); +}; +``` + +api/post-subfolder.js + +```js +// will respont to post requests on /subfolder/ +module.exports = (req, res, next, opts) => { + res.send(opts.foo); +}; +``` diff --git a/test/main.js b/test/main.js index 5f78ae0..ef8e861 100644 --- a/test/main.js +++ b/test/main.js @@ -57,7 +57,8 @@ describe('requestor', () => { 'put-/sub/', 'delete-/sub/', 'all-/sub/', - 'get-/sub/lv1/lv2/lv3/' + 'get-/sub/lv1/lv2/lv3/', + 'all-/sub/root/' ]); }); }); diff --git a/test/sub/all-sub.root.js b/test/sub/all-sub.root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/sub/all-sub.root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +};