当我使用http://tes.com/routes时,它会路由到api => get(' /'),而不是web => get(' /' )。为什么呢?
app.js:
var api = require('./app/routes/routes').api;
var transaction_web = require('./app/routes/routes').web;
app.use('/api/routes', transaction_api);
app.use('/routes', transaction_web);
routes.js:
var api = (function () {
router.get('/', function (req, res, next) {
...
});
return router;
})();
var web = (function () {
router.get('/', function (req, res, next) {
...
});
return router;
})();
module.exports = {
api: api,
web: web
};
答案 0 :(得分:1)
原因是因为这是您添加路线的顺序。
此:
var api = (function () {
router.get('/', function (req, res, next) {
...
});
return router;
})();
与:
相同router.get('/', function (req, res, next) {
...
});
var api = router;
同样的事情发生在你指定web
的另一个区块,所以你最终得到:
router.get('/', function (req, res, next) {
// api route
});
var api = router;
router.get('/', function (req, res, next) {
// web route
});
var web = router;
解决方案是创建单独的路由器实例。例如:
var api = new express.Router();
api.get('/', function (req, res, next) {
// web route
});
var web = new express.Router();
web.get('/', function (req, res, next) {
// web route
});