【发布时间】:2020-04-15 11:39:16
【问题描述】:
我在 Node.js 中学习 Express 并遇到 Router() 允许我们模块化我们的路线。但比我发现这段代码:
// we'll create our routes here
// get an instance of router
var router = express.Router();
...
// route with parameters (http://localhost:8080/hello/:name)
router.get('/hello/:name', function(req, res) {
res.send('hello ' + req.params.name + '!');
});
// apply the routes to our application
app.use('/', router);
让我困惑的是为什么我们需要使用 app.use('/', router);应用路线。也就是说,如果我们使用 app.get('/', router);
【问题讨论】: