README
Install: npm install piroute
Usage: The following example assumes the following folder structure
- lib/
- router/
- index.js
- handlers.js
- router/
- server.js
You might have an application with a server.js such as:
let http = require('http')
let port = 8000
let router = require('./lib/router')
let server = http.createServer((req, res) => {
router(req, res)
})
console.log('Listening on port: ' + port)
server.listen(port)
This will simply forward the incoming requests to lib/router/index.js
In your router you can specify your routes, this file might look like:
let piroute = require('piroute')
let handlers = require('./handlers')
piroute.chart(function (route) {
route('application', { path: '/', default: true }, function () {
route('feed', { path: '/feed' })
route('other', { path: '/other' })
})
})
piroute.navigate(handlers)
module.exports = piroute.sail
Note the second argument when specifying a route will allow you to specify a default route
Your handlers will handle appropriate actions for the routes specified, or default. The handlers.js file might look like:
module.exports = {
feed: function (options, res) {
res.end('Route Feed triggered with options :' + JSON.stringify(options))
},
other: function (options, res) {
res.end('Route Others triggered with options: ' + JSON.stringify(options))
},
application: function (options, res) {
res.end('Application route triggered with options: ' + JSON.stringify(options))
}
}