0

When we call an asynchronous function if we can pass a callback function with parameters. I am unable to understand that do I need to memorize the order of the parameter in the call back function. e.g in express

app.get('/',function(req,res))

How to know the number of the parameters and the what they contain because I watched a tutorial and I know first req and then res.

3
  • 3
    It should be in the API documentation for the library you're using. If not, check the source code. Commented Jul 24, 2018 at 8:55
  • I watched a tutorial and I know first req and then res for 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, otherwise result contains a result of some kind - you'll find this pattern very common in nodejs Commented Jul 24, 2018 at 9:02
  • How do these parameters work meaning first it get the '/' link at the port then it call the function .How are the value to these function are given ? Where do node pass the value to req and res Commented Jul 24, 2018 at 9:08

3 Answers 3

1

When we call an asynchronous function if we can pass a callback function with parameters.

Depends on the function. Modern ones tend to return a Promise instead of accepting a callback.

do I need to memorize the order of the parameter in the call back function.

No, you can look them up.

How to know the number of the parameters and the what they contain

By reading the documentation for the function you are passing the callback to

Sign up to request clarification or add additional context in comments.

1 Comment

Quentin @Quentin #Quentin So I need to look up for the documentation to see the parameters to use and cannot we call a functikn without call back function
1

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

1 Comment

Hello, Mr. Sairohith. The code snippet you posted was very helpful. Regards.
0

app.get or any app.[CRUD Method] takes 2 or 3 params (request, response, next) in the same order. The next is optional

3 Comments

Where can I find the source code of the function get? I went to the GitHub and cannot find the get function. Plz, send me the documentation link because in nodejs.com it is hard for me to read the link
I read the documetation somewhat it was just telling that in app.get (path,callback,[callback]) and said you can pass middleware their or array of middleware. I want to get started with node and I liked it but this topic is confusing me
That's not a CRUD (create, read, update, delete) method though, it's an HTTP method (get, post, delete, ...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.