express-controller-routes
v1.0.6
Published
Module for connecting routes with controllers actions
Downloads
4
Maintainers
Readme
This module makes it easy to create routes connected with controllers actions in kind of 'rails' way
Install
Install this module with
npm install --save express-controller-routes
Useage
Firstly, create folder where all of your controller files will be.
Name of controller file is controller name at the same time. For example file named UserController.js
will create controller with name user
Inside controller file create actions this way
// myapp/controllers/UserController.js
exports.actionName = function(req, res) { /*your action code*/ };
exports.anotherActionName = function(req, res) { /*your action code*/ };
Then in your project file (index.js
propably) define routes this way:
// myapp/index.js
var app = express(); //express app object
var router = require("express-controller-routes")
router(app, "controllers", function(ctrls, get, post, put, del){
get("/user/:id", ctrls.user.actionName);
post("/user/something", ctrls.user.anotherActionName);
put("route...", ctrls.someController.someAction);
delete("route...", ctrls.someOtherController.myAction);
//etc...
});
First argument is Express appliaction object. Second argument is folder name where you hold all your controllers files. Third argument is your function that will add routes. This function takes those parameters (order is important)
- Object that will hold all your controllers with their actions
- Function name for adding get method routes
- Function name for adding post method routes
- Function name for adding put method routes
- Function name for adding delete method routes (name it different way than
delete
as it's reserved keyword