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 "/".
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.resin(req, res)is not theresin(res)function (apple, banana)you could then usebanana.sendFile(...)and it would work fine.