Simple and flexible router publisher for express and restify
npm install --save node-node-lite-router
node-lite-router has a two main implementations to be chosen: restify and express, booth of them share the same api.
To use LiteRouter in restify
const LiteRouter = require("node-lite-router").restify;
const routesDefinition = require("./path_to/routes_definition.js");
let routerHandler = LiteRouter.init({
controllers: controllers,
middlewares: middlewares
});
routerHandler.publishRoutes(routesDefinition).then((router) => {
//do something with the created router
});
To use LiteRouter in express
const LiteRouter = require("node-lite-router").express;
const routesDefinition = require("./path_to/routes_definition.js");
let routerHandler = LiteRouter.init({
controllers: controllers,
middlewares: middlewares
});
routerHandler.publishRoutes(routesDefinition).then((router) => {
//do something with the created router
});
Note that in booth cases LiteRouter.init method takes two parameters: controllers and middlewares, these can be an array of modules(loaded via mod-loader for example) or a string pointing to a directory.
If you specify a directory name, then liteRouter will execute require on the specified path so you may want to use bootstraping for controllers and middlewares.
In order to use node-lite-router you have to use a route definition; a json array used to create a router with the following syntax:
[
{
"httpVerb":"get",
"route":"/tasks",
"action":"TasksController?findAll",
"middlewares": [
"visitCounter"
]
}
]
It also can be expressed as a javascript module
module.exports = [
{
httpVerb:"get",
route:"/tasks",
action:"TasksController?findAll",
middlewares: [
"visitCounter",
(req,res,next)=> {
console.log("Hi there just passing by")
next()
}
]
}
]
The following table show the parameters required to configure a route definition
field | description | Example |
---|---|---|
httpVerb | Http verb to be published in the route | get |
route | Endpoint to be published | /tasks |
action | ControllerName?actionName to be executed | TaskController?findAll |
middlewares | List of middlewares | [ "visitCounter" ] |
As you may noticed the action parameter follows a syntax like taskController?findAll, this is a syntax I've decided to call actionString is a simple way of representing class and method invocation, LiteRouter knows that you are specifying a class?method binding to a route.
The same goes for middlewares if you specify a middleware as a string it will look for them in the middlewares object, however if LiteRouter detects actionString syntax it will try to resolve your middleware as a method from a class.
In this example we define a update action with the verb PUT for a route identified for /tasks/:task_id in the middlewares you can see two entries:
- visitCounter: A simple middleware that is going to bee looked up into middlewares object.
- TasksController?exists: An actionString specifying that TasksController has a method called exists that will be threated as a middleware for the execution stack.
module.exports = [
{
httpVerb:"put",
route:"/tasks/:task_id",
action:"TasksController?update",
middlewares: [
"visitCounter",
"TasksController?exists"
]
}
]
For this example the TasksController will look like this:
class TasksController {
update(req,res) {
//update logic
res.send({..})
}
exits(req,res,next) {
if(exits...) {
return next()
} else {
return res.status(404).send(...)
}
}
}
To have a clear idea of how node-lite-router works you have two examples for running apis with express and restify.
npm test
npm generate-docs
node-lite-router allows you to encapsulate route publishing and middleware organizing as externalized files, this enable a modular development with a fine separation of concerns for your express & restify REST Apis.
Somebody has the name lite-router in the npm registry :D