0

Hi I am trying to understand how callback functions work and I have a question that will clear my doubts. when adding event listeners it is simple

$0.addEventListener("click", function(event){console.log(event)}); 

If i perform a "click" the following should happen.

But what about when there is two parameters in the callback function itself. Like in express. What does it mean?

app.get("/", function(req, res){
    res.sendFile(__dirname + "/page.html")
});

Why can't we just say

app.get("/", function(res){
    res.sendFile(__dirname + "/page.html")
});

one parameter in the event listener function worked but it does not work here. Why is that? What is the use of the "req", if we are already making a request in with the "/".

4
  • app.get("/", ...) doesn't make a request, it registers your callback to handle a GET request to that endpoint. The callback receives the request and the response objects because that's the API, in the same way that the API for an event listener's callback is to receive an event. Commented Sep 10, 2020 at 9:46
  • 2
    The value of the arguments in the callback are determined by their position, not by their name Commented Sep 10, 2020 at 9:46
  • In parameter list, order matters, names don't. The res in (req, res) is not the res in (res) Commented Sep 10, 2020 at 9:46
  • To illustrate, if you used function (apple, banana) you could then use banana.sendFile(...) and it would work fine. Commented Sep 10, 2020 at 9:53

3 Answers 3

1

You can't skip an argument, because JavaScript refers to an argument by position:

app.get("/", function(
  res, // here you would be referring to request
  // actual response can be referenced here
){
  res.sendFile(__dirname + "/page.html")
});

If you don't need a reference to the first argument, the convention is to use an underscore for the name:

app.get("/", function(_, res){
  res.sendFile(__dirname + "/page.html")
});
Sign up to request clarification or add additional context in comments.

Comments

0
app.get("/", function(req, res){
    res.sendFile(__dirname + "/page.html")
});

The first parameter to the function is req and it can contain necessary information such as query string parameters used by the HTTP GET request by the user. The second parameter is used to send back the response to caller.

Since you are using the second parameter res, you can define the first parameter req and ignore it.

Comments

0

You can also use :

app.get("/", function({res}){
  res.sendFile(__dirname + "/page.html")
});

To understand why it works, you need to understand the implementation of Express API.

notice these three interfaces:

interface Request<P = core.ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = core.Query> extends core.Request<P, ResBody, ReqBody, ReqQuery> { }
interface RequestHandler<P = core.ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = core.Query> extends core.RequestHandler<P, ResBody, ReqBody, ReqQuery> { }
interface RequestParamHandler extends core.RequestParamHandler { }

They define the req handler, and as you can see those interfaces generate an initialization values.

Then, when you don't use req it stills works from behind the scene.

So, why using app.get("/", function({res}) works? because then you keep the default values of req by not changing them and notice the compiler that there are few more callback parameters by putting {}.

I hope that explanation helped, pretty hard for me the explain that.

2 Comments

yes it did thank you, but why? I am sorry but I really need to understand this in order to move on.
Hi, I'm sorry I answered a few days later. Please see edit.

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.