micro-r v2.0.6
file structure based router for micro/express
Installation
$ npm i micro-rAbout
micro-r is a small and opinionated routing library utilizing file/folder of the application. Routes are not dynamic and are bound at runtime, rendering "Directory Traversal Attacks" not applicable.
Middlewares can be defined by use() or by dropping index.js files in the desired folders. These must export either a plain middleware function or an array of functions.
Functions
configure(object)
Provides the possibility to change the following defaults.
ext: stringextension of middleware files e.g..ts; default:.jsentry: stringname of middleware files e.g.middleware; default:index
on(method, path, handler)
Register custom handlers to specific paths.
method: stringone of:*, get, post, put, patch, delete, head, optionspath: stringpathname to matchhandler: Functioncallback function receiving req and res as parameters
use(middleware)
Register a middleware function.
middleware: Functione.g.(req, res, next) => next()
has(method, path)
Returns true of the requested route exists.
method: stringone of:*, get, post, put, patch, delete, head, optionspath: stringpathname
chain(middlewares)
Transforms an array of middleware functions into a single middleware function.
middlewares: Array<Function>similar to use() but with arrays
register(basepath, onDone)
Recursively register the given path and its subfolders.
basepath: stringpath to folderonDone: Functionoptional; called if registration done
route(req, res)
Handle the incoming requests.
req: IncomingMessageserver request objectres: ServerResponseserver response object
listener
listener Instance exposes three functions to bind, unbind and emit events.
Events
Following Events are available:
Readyif micro-r is done withregister()Errorif an uncatched error in middleware, route orregistercall occurred
on(event, handler)
Register an event handler.
event: stringname of the eventhandler: functionevent handler
off(event, handler)
Unregister an event handler.
event: stringname of the eventhandler: functionevent handler
emit(event, req, res, payload)
Emit an event with payload.
event: stringname of the eventreq: IncomingMessageserver request objectres: ServerResponseserver response objectpayload: anyany kind of data
Example
Following folder/file structure with default configuration
routes
|
├───api
| index.js
| get.js
|
├───photo
| | index.js
│ │ get.js
│ │ post.js
│ │ delete.js
│ │
│ └───vacation
| index.js
│ get.js
│ post.js
│
└───user
post.jsregisters the following routes
GET:example.com/apiGET:example.com/photoPOST:example.com/photoDELETE:example.com/photoGET:example.com/photo/vacationPOST:example.com/photo/vacationPOST:example.com/user
Vanilla
const {default: router, Event} = require('micro-r');
const {send} = require('micro');
const fallback = (req, res) => {
send(res, '404', 'WOOPS');
};
const {on, use, chain, register, route, listener} = router(fallback);
listener.on(Event.READY, () => console.info('I\'m ready'));
listener.on(Event.ERROR, (req, res, error) => console.error(req, res, error));
register('./routes');
on('get', '/foo', async (req, res) => {
send(res, 200);
});
const middlewares = [
(req, res, next) => {
res.data = [];
next();
},
(req, res, next) => {
res.data.push('foobar');
next();
},
];
on('get', '/custom', chain(middlewares)(async (req, res) => {
// res.data === ['foobar']
send(res, 200);
}));
module.exports = route;Express
const {default: router, Event} = require('micro-r');
const express = require('express');
const app = express();
const fallback = (req, res) => {
send(res, '404', 'WOOPS');
};
const {register, route, listener} = router(fallback);
register('./routes', () => console.info('I\'m ready'));
app.use(route);
app.listen(3000);Licence
MIT License
Copyright (c) 2019 Oleg Kamlowski
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago