restify-better-router
v1.0.3
Published
A clean way to organize routes for the restify router
Downloads
6
Readme
A Better Restify Router
A clean way to organize routes for the restify router
Installation
$ npm i -S restify-better-router
Command not working? Follow this tutorial.
Motivation
This package allows you to define your routes using a Route object, making it very easy to attach routes to your restify server. In larger applications which have many routes organizing routes off of file structure may be more optimal than one large file.
Example
File user.js
const Route = require('restify-better-router')
module.exports.name = new Route()
.get('/name/:name')
.addHandler(function (req, res, next) {
res.send('Hello ' + req.params.name)
next()
})
File place.js
const Route = require('restify-better-router')
module.exports.name = new Route()
.get('/place/:place')
.addHandler(function (req, res, next) {
res.send('Hello from ' + req.params.place)
next()
})
File api.js
const restify = require('restify');
const Route = require('restify-better-router')
var server = restify.createServer()
// sub routes
const name = require('./name')
const place = require('./place')
var api = new Route()
.setPath('/api')
.addRoutes([name, place])
.attach(server)
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url);
});
Creating a route
A router object is an isolated instance of routes. The router interface matches the interface for adding routes to a restify server:
var Router = require('restify-router').Router;
var routerInstance = new Router();
var restify = require('restify');
function respond(req, res, next) {
res.send('hello ' + req.params.name);
next();
}
// add a route like you would on a restify server instance
routerInstance.get('/hello/:name', respond);
var server = restify.createServer();
// add all routes registered in the router to this server instance
routerInstance.applyRoutes(server);
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url);
});
Usage
Upon creating a new route an object can be given as a shortcut to the chainable methods
new Route({
name: {string} Given route name
version: {string|string[]} Version require for route
path: {string} Path for a route
method: {string} GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS
handler: {Function|Function[]} A handler function, or Array of handler functions
})
If you prefer, fluent methods can be used instead of the object.
Assuming we create a route object like this var r = new Route()
.
One can set path and method using...
r.setPath(path)
Sets the path for route aspath
r.get(path)
Sets GET method, with a specifiedpath
r.post(path)
Sets POST method, with a specifiedpath
r.put(path)
Sets PUT method, with a specifiedpath
r.patch(path)
Sets PATCH method, with a specifiedpath
r.head(path)
Sets HEAD method, with a specifiedpath
r.del(path)
orr.delete(path)
Sets DELETE method, with a specifiedpath
r.opts(path)
orr.options(path)
Sets OPTIONS method, with a specifiedpath
One can modify the route's other attributes using...
r.setName(name)
Given route the namename
r.setVersion(verion)
Sets a route's version toversion
(can be an array of versions)r.addHandler(handler)
Addshandler
as the route's function to the router.addRoute(route)
Adds a child route to be prefixed by this route's pathr.addRoutes(routes)
Adds an array of children routesr.attach(server)
Attachés this route and its children toserver
Attaching routes to a server
Once you have made and grouped your routes, you can attach them to the restify server to become live.
const Route = require('restify-better-router')
const restify = require('restify')
var r = new Route({
name: 'route',
method: 'get',
handler: function (req, res, next) {
res.send('Hello World!')
next()
}
})
var server = restify.createServer()
// r will now be seen by the server
r.attach(server)
// start server
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url)
})
Influenced by restify-router.