My app uses Node.js and Express 4 and has the following code in the app.js file located in the root directory:

var express = require('express');

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

app.use('/', index);
app.use('/users', users);

The routes folder in the root directory contains an index.js file with the following code:

var express = require('express');
var router = express.Router();

router.get('/', function (req, res, next) {
    res.render('index');
});

module.exports = router;

The routes folder also contains a users.js file with the following code:

var express = require('express');
var router = express.Router();

router.get('/', function (req, res, next) {
    res.render('users');
});

module.exports = router;

I’m new to Express and Node.js. It’s my understanding that this structure causes browser requests to domainName.com to run the index.js file, while requests to domainName.com/users will run the users.js file, resulting in different views being rendered in the browser.

What is the purpose of organizing the code this way? (i.e. why is app.use() in app.js used to isolate each router.get(path, callback) into it’s own respective module?)

Is the commonly-accepted practice to organize routes differently for a single page app versus a website consisting of mostly static text, such as a blog? If so, how?

5

When you only have two routes, it makes no sense to separate them in this way. However, when you consider a real application with many routes, it begins to make more sense.

/routes/user.js

var express = require('express');
var router = express.Router();
var User = require('../models/user');

router.get('/', function (req, res, next) {
    res.render('users');
});

router.param('userid', function (req, res, next, userid) {
    req.user = User.findById(userid);
    req.userid = userid;
});

router.get('/:userid', function (req, res, next) {
    req.user.exec(function (err, user) {
        if (err) { return next(err); }
        res.render('user', {user: user});
    });
});

router.put('/:userid', function (req, res, next) {
    User.findByIdAndUpdate(req.userid, req.body, function (err) {
        if (err) { return next(err); }
        res.render('user-updated');
    });
});

router.delete('/:userid', function (req, res, next) {
    User.findByIdAndDelete(req.userid, function (err) {
        if (err) { return next(err); }
        res.render('user-deleted');
    });
});

module.exports = router;

Having all of that in app.js will get out of hand very quickly, even more so once you have more models than just user.

Also note that this isn’t the only way to organize your routes/models/views nor is it the “best” way for all cases. You’ll have you decide for yourself what will work best in each application.


Note, the code in the sample is of course all just sample code that probably won’t work. My model methods were based off of mongoose model methods.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật