When you create an Express application you get a routes folder. All routes are registered in the app.js file. However the logic on what happens is located in the files of the routes folder. Is this a synonym for controller folders in other frameworks? Is this the location of where you should add the request/response logic?
When you create an Express application you get a routes folder. All routes are registered in the app.js file. However the logic on what happens is located in the files of the routes folder. Is this a synonym for controller folders in other frameworks? Is this the location of where you should add the request/response logic?
Share Improve this question asked Jan 4, 2013 at 15:30 LuckyLukeLuckyLuke 49.1k87 gold badges279 silver badges446 bronze badges1 Answer
Reset to default 16Yes, is kind of the same thing as a controller folder. IMO, you better use different files as you would with controllers in another language because when the application is getting bigger it's hard to understand the code when all the request/response logic is in the same file.
Example :
app.js :
var express = require('express'),
employees = require('./routes/employee');
var app = express();
app.get('/employees', employees.findAll);
app.get('/employees/:id', employees.findById);
app.listen(80);
routes/employee.js :
exports.findAll = function(req, res) {
res.send([{name:'name1'}, {name:'name2'}, {name:'name3'}]);
};
exports.findById = function(req, res) {
res.send({id:req.params.id, name: "The Name", description: "description"});
};