I am a tyro in node and express, learning the things. I was reading Express's documentation at this link: https://expressjs.com/en/guide/routing.html and in the Route Handlers section of this page, it says:
You can provide multiple callback functions that behave like middleware to handle a request. The only exception is that these callbacks might invoke next('route') to bypass the remaining route callbacks.
The examples given on this page doesn't contain any next() with arguments in it.
I tried implementing it and passing some path as an argument in this function but it behaves strangely and prints that argument in the console and also sends it to the browser.
Below is my code:
const express = require('express');
const port = 8000;
const app = express();
app.get('/', (req, res, next) => {
console.log("I am the first one");
next('/demo');
}, function(req, res, next){
console.log("I am the second one");
});
app.get('/demo', (req, res) => {
console.log("Good!");
});
app.listen(port, (err) => {
if(err){
console.log("ERROS: ",err);
}
console.log("Express server is runnig on port: ",port);
});
In the browser, I typed http://localhost:8000/
The output in console is:
I am the first one
/demo
the next('/demo') function call doesn't cause /demo route handler to run.
Where am I going wrong?
Am I getting it right?
Actually I am not able to understand how this argument thing works with the next() function.
Since the documentation page does not have any example with arguments in function, Can anyone please explain how this next() function work with arguments through an example?
Thanks in advance for any help you are able to provide