Try to run this code. You can see there are 2 middleware functions before we get inside the get method(Passed like array of functions). Another middleware after the get method. This will give you a rudimentary understanding of the sequential way request gets handled and how request can be manipulated.
var express = require("express");
var app = express();
var port = 3000;
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var middleware1 = function(req,res,next){
console.log("Before Get method middleware")
console.log("middleware1");
next();
}
var middleware2 = function(req,res,next){
console.log("middleware2");
next();
}
app.get("/", [middleware1 , middleware2],(req, res, next) => {
console.log("inside Get middleware")
req['newparam'] = "somevalue"; // or somecode modiffication to request object
next(); //calls the next middleware
});
app.use('/',function(req,res){
console.log("After Get method middleware")
console.log(req['newparam']); //the added new param can be seen
res.send("Home page");
})
app.listen(port, () => {
console.log("Server listening on port " + port);
});
I watched a tutorial and I know first req and then resfor that particular function ... but, in general, the API's in node that use callbacks, the callback usually takes(err, result)- if err is not falsey - it contains an error, otherwiseresultcontains a result of some kind - you'll find this pattern very common in nodejs