0
我應該用什麼:節點express.Router()航線()VS express.route()
express.Router().route()
或
express.route()
?
是不是真的express.Router().route()
有人棄用?
我應該用什麼:節點express.Router()航線()VS express.route()
express.Router().route()
或
express.route()
?
是不是真的express.Router().route()
有人棄用?
對於當前版本的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'));
})
謝謝...還有一個疑問:我可以用一個':optionalParameter'用'express.Router()航線()'?我無法在文檔中找到它... app.route()可以使用'?'在param名稱之後... – MarcoS