title: Breaking express router into multiple files
body: <p><a href="https://www.google.com/webhp?sourceid=chrome-instant&amp;ion=1&amp;espv=2&amp;ie=UTF-8#q=how%20to%20break%20an%20express%20router%20into%20different%20files" rel="nofollow">I googled this question</a> 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 <a href="http://expressjs.com/guide/routing.html" rel="nofollow">Express documentation</a> 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?</p>
  
  <p><strong>router.js</strong></p>
  
  <pre><code>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;
  </code></pre>