Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 598
Ross Avatar asked Dec 08 '11 12:12

Ross


People also ask

What does next do in Req Res next?

Middleware functions are functions that have access to the request object ( req ), the response object ( res ), and the next function in the application's request-response cycle. The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.

What is next () in node JS?

In this is article we will see when to use next() and return next() in NodeJS. Features: next() : It will run or execute the code after all the middleware function is finished. return next() : By using return next it will jump out the callback immediately and the code below return next() will be unreachable.

What is next parameter in Express?

Next simply allows the next route handler in line to handle the request. In this case, if the user id exists, it will likely use res. send to complete the request. If it doesn't exist, there is likely another handler that will issue an error and complete the request then.

What does res sendFile do?

The res. sendFile() function basically transfers the file at the given path and it sets the Content-Type response HTTP header field based on the filename extension.


2 Answers

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.)

like image 187
T.J. Crowder Avatar answered Oct 05 '22 23:10

T.J. Crowder


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.

like image 26
alessioalex Avatar answered Oct 05 '22 23:10

alessioalex