大多数情况下,当我们想在node.js Express应用程序中拆分路由器时,我们确实喜欢这个......
var express = require('express')
var router = express.Router()
// middleware that is specific to this router
router.use(function timeLog (req, res, next) {
console.log('Time: ', Date.now())
next()
})
// define the home page route
router.get('/', function (req, res) {
res.send('Birds home page')
})
// define the about route
router.get('/about', function (req, res) {
res.send('About birds')
})
module.exports = router
然后,在应用程序中加载路由器模块:
var birds = require('./birds')
// ...
app.use('/birds', birds)
但是,在 express-subdomain 中,有没有办法像上面那样在子域中拆分路由器呢???
var router = express.Router();
//api specific routes
router.get('/', function(req, res) {
res.send('Welcome to our API!');
});
router.get('/birds', function(req, res) {
res.json([
{ name: "Brian" }
]);
});
然后,在应用程序中加载路由器模块:(这不允许我拆分更多的路由器???)
app.use(subdomain('api', router));
答案 0 :(得分:0)
使用router.use(),解决了我的问题。
MyAdapter