最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Routes folder in Express - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 16

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"});
};
发布评论

评论列表(0)

  1. 暂无评论