Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routes folder in Express

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?

like image 591
LuckyLuke Avatar asked Jan 04 '13 15:01

LuckyLuke


1 Answers

Yes, 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"});
};
like image 149
Jean-Philippe Bond Avatar answered Oct 08 '22 03:10

Jean-Philippe Bond