简体   繁体   English

这是如何运作的? Express(err,req,res,next)或(req,res,next)中使用的可选第一个参数

[英]How does this work? Optional first argument used in Express (err, req, res, next) or (req, res, next)

With Express / Connect I can set up an middleware function in either of the formats: 使用Express / Connect,我可以使用以下任一格式设置中间件功能:

function(req, res, next) // first argument will be a request

Or 要么

function(err, req, res, next) // first argument will be an error

Stepping back from Express, Connect, to basic JavaScript: I don't understand is how this is possible to have an optional first argument ? 退出Express,Connect,再到基本JavaScript:我不明白如何有可选的第一个参数

How can express know that my function will accept an err object first? 怎么能表达我的函数会先接受err对象? I thought to make this possible the structure would have to be like the following: 我想这可能使结构必须如下:

function(req, res, next, err)

Am I missing something basic here? 我错过了一些基本的东西吗? Is it possible to query how many arguments a function is expecting? 是否可以查询函数期望的参数数量?

Edit: thanks for the answers below. 编辑:感谢下面的答案。 but the middleware function is passed to express, so the arguments variable is not valid. 但是中间件函数被传递给express,因此arguments变量无效。 although length is correct... I think I have figured it out, would be good to have confirmation to whether this is the case. 虽然length是正确的...我想我已经弄明白了,最好确认是否是这种情况。 Example below: 示例如下:

var fn;

fn = function (one, two) {};
console.log(fn.length); // 2

fn = function (one, two, three) {};
console.log(fn.length); // 3

I think I have figured it out, would be good to have confirmation to whether this is the case 我想我已经弄明白了,最好确认是否是这种情况

 var fn; fn = function (one, two) {}; console.log(fn.length); // 2 fn = function (one, two, three) {}; console.log(fn.length); // 3 

Yes, that's correct. 对,那是正确的。 The length property of a Function instance is the number of formal parameters (declared arguments) it has. Function实例的length属性是它具有的形式参数 (声明的参数)的数量。 This is hidden away in Section 13.2 of the spec, steps 14 and 15. 这隐藏在规范第13.2节 ,步骤14和15中。

So it's quite easy for the code calling the function to check fn.length and pass it the optional first argument, or not, depending on that. 因此,调用函数的代码很容易检查fn.length并将可选的第一个参数传递给它,具体取决于那个。 This does mean, of course, that it's entirely possible to write a function that would handle the four-argument version, but fools the framework by using arguments rather than formal parameters. 这也意味着,当然,这是完全可能的编写处理四个参数版本的功能,但蠢人由使用该框架arguments ,而不是正式的参数。 So you wouldn't do that. 所以你不会这样做。 :-) :-)

(Apologies for misreading your question the first time.) (抱歉第一次误读你的问题。)

The first function has 3 arguments, the second one has 4 arguments, so Express/Connect looks at the number of arguments. 第一个函数有3个参数,第二个函数有4个参数,因此Express / Connect查看参数的数量。

It's not possible to switch between arguments. 在参数之间切换是不可能的。

Express spends quite a long time looking at the arity of the function; Express花了很长时间看这个函数的arity ; ie how many arguments that are in there: 即那里有多少个参数:

function foo(err, req, res, next) {
    if (arguments.length === 3) { // err isn't there
        next = res;
        res = req;
        req = err;
    }
    ...
}

So. 所以。 Detect how many arguments there are, and shuffle variables accordingly. 检测有多少参数,并相应地改变变量。 I've even seen some Node.js-modules that does the automatically for you. 我甚至见过一些自动为你做的Node.js模块。

And while I haven't checked up recently, there were some cases where messing with the arguments -builtin would disable all optimizations in V8. 虽然我最近没有检查过,但在某些情况下,弄乱arguments -builtin会禁用V8中的所有优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM