0
很多時候,我們希望在node.js中拆分路由器快速應用,我們做這樣的時候...拆分路由器在快遞子域
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)
但是,在快速子域,有沒有什麼辦法可以像上面那樣在子域中爲鳥類分割路由器呢?
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));