什么是Node.js post方法中的第二个参数 [英] What is second parameter in nodejs post method
问题描述
在我的代码中,我编写了这样的代码
In my code I have code written like this
router.post('/', publicShare, function(req, res, next) {
我签入了文档,但没有找到第二个参数publicShare
的原因?
I check in documents but not found why second param publicShare
is here?
publicShare
看起来像
var publicShare = function(req, res, next) {
if (condition1) {
if (condition2) {
res.status(400).send({success:false});
} else {
next();
}
} else {
if (condition3) {
res.status(401).send({success:false});
} else
next();
}
};
请帮助我理解.
推荐答案
You can check route handlers which accepts array of callbacks which just behaves like a middleware. Example from the docs:
app.get('/example/d', [cb0, cb1], function (req, res, next) {
因此,在您的情况下,publicShare
可以是回调数组,也可以是签名只是接受req
,res
和next
作为参数的回调的回调.因此,您也可以像这样使用:
So, in your case publicShare
can be array of callbacks or just a callback which signature is just a callback accepting req
, res
, and next
as parameter. So, you can also use like:
app.get('/', function(req, res, next){}, function(req, res, next){}, ...
为了简单起见,您将使用回调数组:
And for easier, you would use an array of callbacks:
app.get('/',[cb1, cb2, cb3])
其中cb1,cb2和cb3是带有请求,响应和下一个参数的回调.它允许您一个一个地运行. cb1->执行日志1,然后cb2->执行日志2,cb3->执行日志3,依此类推.
Where cb1, cb2, and cb3 are the callbacks with request, response and next parameters. It allows you to run one by one. cb1 -> do log 1, then cb2 -> do log 2, cb3 -> do log 3 and so on.
我将通过一个示例来简化此操作:
您要喝水.
1)cb1:购买一瓶水.
1) cb1: Purchase a jar of water.
2)cb2:在水桶或广口瓶中加入几滴水.
2) cb2: Add few water drops in the bucket or jar.
3)cb3:将其煮沸.
3) cb3: Boil it.
然后,轮到您了.喝!
这篇关于什么是Node.js post方法中的第二个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!