2019-12-11 09:32:40 +01:00
|
|
|
# Requestor
|
2019-12-11 12:01:34 +01:00
|
|
|
|
|
|
|
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
|
2019-12-11 12:47:42 +01:00
|
|
|
// will respond to get requests on /
|
2019-12-11 12:01:34 +01:00
|
|
|
module.exports = (req, res, next, opts) => {
|
|
|
|
res.send(opts.foo);
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
api/post-subfolder.js
|
|
|
|
|
|
|
|
```js
|
2019-12-11 12:47:42 +01:00
|
|
|
// will respond to post requests on /subfolder/
|
2019-12-11 12:01:34 +01:00
|
|
|
module.exports = (req, res, next, opts) => {
|
|
|
|
res.send(opts.foo);
|
|
|
|
};
|
|
|
|
```
|
2019-12-11 12:47:42 +01:00
|
|
|
|
|
|
|
optionally, you can set a subdirectory for all requests
|
|
|
|
|
|
|
|
```js
|
|
|
|
//...
|
|
|
|
|
|
|
|
requestor(app, 'api', {foo: 'bar'}, 'subdir');
|
|
|
|
|
|
|
|
// requests that before responded to /foo/bar/ will now respond to /subdir/foo/bar/
|
|
|
|
```
|