configrouter
v0.0.1
Published
Flexible router for quickly building/testing web services.
Downloads
3
Readme
Configrouter
Flexible router for quickly building/testing web services. Powered by route-parser.
Installation
npm install configrouter
Usage
First import the module into your code.
let ConfigRouter = require('configrouter');
Then define your routes following this simple schema.
handler = Function(req, res, params)
Object {
String httpMethod1: Object {
String /route1: handler, //optional
String /route2: handler, //optional
String /noMatch: handler //required
},
}
Example:
let myHandler = (req, res, params) => {
if (params.number === '42') {
res.end('Hello World');
}
};
let routes = {
'GET': {
'/': myHandler,
'/:number': myHandler,
'/noMatch': (req, res) => {
//Handle request
},
},
'POST': {
'/noMatch': (req, res) => {
//Handle request
},
},
Routes resolve in insertion order. In the case of duplicate entries, the latter route wins.
Best practice is to define your routes/handlers in a separate file, then import it into your server.
const ConfigRouter = require('configrouter');
const routes = require('path/to/routes.js')
let router = new ConfigRouter(routes);
let server = http.createServer((req, res) => {
router.route(req, res);
});