我应该使用什么:
express.Router().route()
或
express.route()
是否真的express.Router().route()
被弃用?
答案 0 :(得分:2)
对于当前版本的Express,您应该使用express.Router().route()
。请参阅快速documentation以进行确认。 express.Router().route()
未被折旧。
例如:
var router = express.Router();
router.param('user_id', function(req, res, next, id) {
// sample user, would actually fetch from DB, etc...
req.user = {
id: id,
name: 'TJ'
};
next();
});
router.route('/users/:user_id')
.all(function(req, res, next) {
// runs for all HTTP verbs first
// think of it as route specific middleware!
next();
})
.get(function(req, res, next) {
res.json(req.user);
})
.put(function(req, res, next) {
// just an example of maybe updating the user
req.user.name = req.params.name;
// save user ... etc
res.json(req.user);
})
.post(function(req, res, next) {
next(new Error('not implemented'));
})
.delete(function(req, res, next) {
next(new Error('not implemented'));
})
答案 1 :(得分:1)
Router.route()可用于可链接的路由。 含义:您为所有方法使用一个API,可以将其写入.route()。
var app = express.Router();
app.route('/test')
.get(function (req, res) {
//code
})
.post(function (req, res) {
//code
})
.put(function (req, res) {
//code
})