chopper draw lion4 chopper draw lion4 -4 years ago 87
Javascript Question

Breaking express router into multiple files

I googled this question and was initially quite hopeful at the number of responses. That hopefulness quickly turned into disappointment as I read through the first 4 links, as well as the Express documentation and could not find any viable resolutions to my inquiry. I want to break my routes into two files: One file for GET request and one file for POST request. Many articles on the internet hint at the ability to do this, or they state how to do it but in a very context specific application. Can someone please provide a generalized and fully complete way to break an Express router into multiple files with code examples?

router.js

var express = require('express');
var models = require('../db/models.js');
var util = require('../core/utilities.js');
var router = express.Router();

router.get('/messages', function (req, res) {
util.log('RECEIVED a GET request', req.query);

models.retrieveMarks(req.query)
.then(function(msg) {
util.log('SENT message array to user', msg);
res.status(200);
res.send(msg);
}, function(err) {
util.log('SENT error code to user', err);
res.sendStatus(400);
});
});

router.post('/messages', function(req, res) {
util.log('RECEIVED a POST request', req.body);

//input: {x: float, y: float, z: float, message: string, userToken: string}
models.createMessage(req.body)
.then(function(msg) {
util.log('SENT success code to user', msg);
res.sendStatus(201);
}, function(err) {
util.log('SENT error code to user', err);
res.sendStatus(400);
});
});

//input: {userToken: string}
router.post('/usertoken', function (req, res) {
var token = req.body.userToken;
util.log('RECEIVED a new user token from user', token);

models.createUser(token)
.then(function(user) {
util.log('SENT success code to user', user);
res.sendStatus(201);
}, function(err) {
util.log('SENT error code to user', err);
res.sendStatus(400);
});
});

//input: {messageId: string, userToken: string}
router.post('/upvote', function (req, res) {
util.log('RECEIVED upvote request', req.body);
models.createVote(+req.body.messageId, req.body.userToken);
res.sendStatus(200);
});

//input: {messageId: string, message: string}
router.post('/comment', function (req, res) {
util.log("RECEIVED comment", req.body);
models.createComment(req.body);
res.sendStatus(200);
});

router.get('/comment', function(req, res) {
util.log('RECEIVED a GET request', req.query);
models.retrieveComments(+req.query.messageId)
.then(function(comments) {
util.log('SENT comments to user', comments);
res.status(200);
res.send(comments);
}, function(err) {
util.log('SENT error code to user', err);
res.sendStatus(400);
});
});

router.use(express.static(__dirname + '/../landing'));

module.exports = router;

Answer Source

One solution, with a shared router:

// router.js
var router = express.Router();
require('./get-routes')(router);
require('./post-routes')(router);

// get-routes.js
module.exports = function(router) {
  router.get(...);
};

// post-routes.js
module.exports = function(router) {
  router.post(...);
};

Another solution, with nested routers:

// router.js
var router = module.exports = require('express').Router();
router.use(require('./get-routes'));
router.use(require('./post-routes'));

// get-routes.js
var router = module.exports = require('express').Router();

router.get(...);

// post-routes.js
var router = module.exports = require('express').Router();

router.post(...);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download