在路线中分离api和web

时间:2015-09-15 15:42:42

标签: node.js express

尝试分离api和web路由:

路线/ index.js

var api = function () {
    router.get('/:id', function (req, res, next) {
       ...
    });

    return router;
}

var web = function () {
    router.get('/:id', function (req, res, next) {
       ...
    });

    return router;
}

module.exports = {
    api: api,
    web: web
};

app.js

var indexAPI = require('./app/routes/accounts').api;
var indexWeb = require('./app/routes/accounts').web;

app.use('/api/index', indexAPI);

但它没有成功路由。

1 个答案:

答案 0 :(得分:0)

我已将其更改为正常工作:

var api = (function () {
    router.get('/:id', function (req, res, next) {
       ...
    });

    return router;
})();

var web = (function () {
    router.get('/:id', function (req, res, next) {
       ...
    });

    return router;
})();

module.exports = {
    api: api,
    web: web
};