Node.js node express.Router().route()与express.route()的比较
我应该使用什么:Node.js node express.Router().route()与express.route()的比较,node.js,express,routes,Node.js,Express,Routes,我应该使用什么: express.Router().route() 或 ? 这是真的吗express.Router().route()在某种程度上被弃用了?对于当前版本的express,您应该使用express.Router().route()。请看快递确认express.Router().route()不折旧 例如: var router = express.Router(); router.param('user_id', function(req, res, next, id) {
express.Router().route()
或
?
这是真的吗
express.Router().route()
在某种程度上被弃用了?对于当前版本的express,您应该使用express.Router().route()
。请看快递确认<代码>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'));
})
route.route()可用于可链接路由。
意思是:所有方法都有一个API,可以在.route()中编写
谢谢还有一个疑问:我可以将
:optionalParameter
与express.Router().route()一起使用吗?我在文件里找不到。。。app.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'));
})
var app = express.Router();
app.route('/test')
.get(function (req, res) {
//code
})
.post(function (req, res) {
//code
})
.put(function (req, res) {
//code
})