简体   繁体   中英

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:

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 ?

How can express know that my function will accept an err object first? 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. although length is correct... I think I have figured it out, would be good to have confirmation to whether this is the case. 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. This is hidden away in Section 13.2 of the spec, steps 14 and 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. 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. 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.

It's not possible to switch between arguments.

Express spends quite a long time looking at the arity of the function; 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.

And while I haven't checked up recently, there were some cases where messing with the arguments -builtin would disable all optimizations in V8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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