0

Here is the Class: http.Server documentation

enter image description here

[Q1]: Is function(request,response){...} the request Event's Listener?

[Q2]: if It is,from my understanding,whenever there is a request, the listener gets called?

[Q3]: According to the following,

enter image description here

Does it mean if I pass in a listener as a parameter, it will be registered on request event automatically?Can I pass in any function Object?

If I pass in function(request,response){...} ,is it the so called callback function when it's triggered by a request event?

Not quite sure about the definition of callback function

1 Answer 1

2

requestListener is a function you pass to the http.createServer() method. If you pass that function, then it will get called on every incoming request that the http server receives.

And, when it gets called, it will be called with two arguments, a request object and a response object in that order.

The requestListener function that you pass to http.createServer() is referred to as a callback function because you're passing a function that will get called back later by some other code.

Here's a simple example:

// Create an simple http server that knows about one request URL
var server = http.createServer(function(req, res) {
    if (req.url === "/") {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('okay');
    } else {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('not okay');
    }
});

[Q1]: Is function(request,response){...} the request Event's Listener?

It is a callback function that is automatically registered for the request evnet and thus is called for every incoming http request.

[Q2]: if It is,from my understanding, whenever there is a request, the listener gets called?

Yes.

[Q3]: Does it mean if I pass in a listener as a parameter, it will be registered on request event automatically?

Yes.

Can I pass in any function Object?

Yes. You must declare the arguments appropriately as request and response in that order, but you can name those arguments anything you want. Their values are passed as the first and second arguments. The names of the arguments are whatever you choose to use in your function. A usual convention is to use (req, res) or (request, response) as this makes your code more recognizable to other node.js developers.

If I pass in function(request,response){...} ,is it the so called callback function when it's triggered by a request event?

Yes.

Not quite sure about the definition of callback function

It is just a function that you pass as an argument to another function. It can be either a function named that is defined as a named function or it can be an inline anonymous function as shows in the example above. It does not matter which it is. It is called a callback function because it will be "called back" by some other code at some time in the future.

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

Comments

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.