Express 4.X建议将app.use()用于需要在get,post和其他http请求中执行不同操作的URL。
例如:-
const app = express();
app.route('/events').all(function(req, res, next) {
// runs for all HTTP verbs first
// think of it as route specific middleware!
})
.get(function(req, res, next) {
res.json(...);
})
.post(function(req, res, next) {
// maybe add a new event...
});
我的问题是:-
(1)对于以上编写的代码,我可以以相同的方式使用router.route()吗?
(2)上面编写的以下代码将与router.route()函数而不是app.route()一起使用吗?
// Will the following code work ?
const app = express();
const router = app.Router();
router.route('/events').all(function(req, res, next) {
// runs for all HTTP verbs first
// think of it as route specific middleware!
})
.get(function(req, res, next) {
res.json(...);
})
.post(function(req, res, next) {
// maybe add a new event...
});