Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nodejs Express, routes with multiple parameters in function?

I typically see Express routes as follows and have questions about multiple parameters passed to app.get().

I understand typical usage with callback function function(req, res) however am confused about the cases where the callback has multiple parameters or app.get() receives multiple parameters.

Standard and documented:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

app.get('/', function(req, res){
});

However, I also see other usage with multiple parameters:

app.get('/user/:id/edit', a, b, function(req, res){
});

I’m assuming function a(req, res), function b(req, res), so above is just a list of callbacks. Is the response just appended left to right?

app.get('/example/b', (req, res, next) => {
})

In this case the callback have an extra argument next. Does the Javascript compiler always provide next() and it’s simply dropped in the above handlers where they have no third argument?

Thanks

>Solution :

However, I also see other usage with multiple parameters:

Also documented:

app.get(path, callback [, callback ...])

(with the [] indicating optional arguments and the ... indicating repeating ones).

I’m assuming function a(req, res), function b(req, res), so above is just a list of callbacks. Is the response just appended left to right?

The functions are called, in sequence, left to right. Each will have the usual arguments passed to them.

In this case the callback have an extra argument next. Does the Javascript compiler always provide next() and it’s simply dropped in the above handlers where they have no third argument?

The arguments are passed by a function that is part of Express, not the compiler per se.

Any function that doesn’t register a parameter for an argument will drop it (mostly; see the arguments object):

function foo(a, b, c) {
  console.log(a + b);
}

foo(1, 2, 4, 8);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading