Routify is a minimal http routing module for node.
It mixes the simplicity of tiny-route with the parameter support of paramify.
npm install http-routify
var http = require('http')
var Stack = require('stack')
var router = require('http-routify')
http
.createServer(
Stack(
router.get('/', (req, res) => res.end('Root')),
router.post('/blog/posts/', (req, res) => {
res.statusCode = 201
res.end('Created blog post')
}),
router.get('/blog/posts/:slug', (req, res) => {
res.end(`We support parameters as well: ${req.params.slug}`)
})
)
)
.listen(1337)
The example uses Stack and requires node 8 or above.
npm start
Open up a browser or command line and test the routes on http://localhost:1337
Running the tests requires node 8 or above.
npm test
npm run test:watch
router.verb(path,handler[,middleware])
-
path: <string>
A path pattern relative to the root. Example:/api/users/:id
-
handler: <function>
The handler to execute when the request matches. See Handlers. -
middleware: <array>
[Optional] Array of middleware to execute before executing the handler. Overrides all middleware globally applied to the router. See Middleware.
The router currently supports the following HTTP verbs
Matches HTTP GET requests
Matches HTTP POST requests
Matches HTTP PUT requests
Matches HTTP DELETE requests
router.all
is a bit special. It matches any HTTP verb and only matches the url against the path.
Each route handler needs to be of the form:
function handler (req, res, next) {
// Either handle the request here using `req` and `res`
// or call `next()` to pass control to next route
}
The next
function is only required in order to delegate handling of a request to the next route in the stack and may be omitted.
The router supports defining middleware that wraps around each route handler, creating a chain of behaviors to be executed. Middleware can be defined globally and overridden on each route.
router.use(middleware)
middleware: <function>
A function that wraps around all route handlers. Middleware may also be specified as an array of functions.
A middleware function needs to be in the form of:
function middleware (inner) {
return function (req, res, next) {
inner(req, res, next)
}
}
In general, a middleware should always call any inner functions (middleware or handler) passed to it. However, there are exceptions where you might want to skip calling inner behaviors and end the request in your middleware.
To run code after the inner behavior has executed you may use async/await in handlers and middleware. See example below.
router.use(inner => {
return (req, res, next) => {
console.log('Before handler')
inner(req, res, next)
}
})
router.use(inner => {
return async (req, res, next) => {
console.log('Before handler')
await inner(req, res, next)
console.log('After handler')
}
})
Please report any bugs by opening an issues on Github.