将Express路由器分成多个文件

时间:2015-04-11 13:52:27

标签: javascript express

I googled this question并且最初对答复的数量充满希望。当我通读前4个链接以及Express documentation并且无法找到任何可行的解决方案时,这种希望很快变成了失望。我想将我的路由分成两个文件:一个用于GET请求的文件和一个用于POST请求的文件。互联网上的许多文章暗示了这样做的能力,或者他们说明了如何做到这一点,但是在特定于上下文的应用程序中。有人可以提供一个通用且完全完整的方法,通过代码示例将Express路由器分成多个文件吗?

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;

1 个答案:

答案 0 :(得分:2)

使用共享路由器的一种解决方案:

// 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(...);
};

使用嵌套路由器的另一种解决方案:

// 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(...);