Express.js多种方法 [英] Express.js multiple methods
本文介绍了Express.js多种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此在Express中,您可以执行以下操作:
So in Express you can do:
app.get('/logo/:version/:name', function (req, res, next) {
// Do something
}
和
app.all('/logo/:version/:name', function (req, res) {
// Do something
}
是否只有两种方法(即GET和HEAD)?如:
Is there a way to just have two methods (ie. GET and HEAD)? Such as:
app.get.head('/logo/:version/:name', function (req, res, next) {
// Do something
}
推荐答案
只需拔出匿名函数并为其命名:
Just pull out the anonymous function and give it a name:
function myRouteHandler(req, res, next) {
// Do something
}
app.get('/logo/:version/:name', myRouteHandler);
app.head('/logo/:version/:name', myRouteHandler);
或使用常规的中间件功能并检查req.method
:
Or use a general middleware function and check the req.method
:
app.use('/logo/:version/:name', function(req, res, next) {
if (req.method === 'GET' || req.method === 'HEAD') {
// Do something
} else
next();
});
这篇关于Express.js多种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文