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:
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);