Node.js httpserver.listen method ambiguity

I've worked in Node.js and am wondering what exactly the listener does in terms of eventloop. If I had a long request, does that mean the server will never listen as it can only do one job at a time.

var http = require('http');
function handleRequest(request, response) {
    response.end('Some Response at ' + request.url);
}
var server = http.createServer(handleRequest);
server.listen(8083, function() {
    console.log('Listening...')
})

      

Is server.listen listening for any event?

+3


source to share


3 answers


You can think of server.listen()

as starting your web server so that it actually listens for incoming requests at the TCP level. From the node.js http documentation for .listen()

:

Start accepting connections on the specified port and hostname.

The callback passed to server.listen()

is optional. It is called only once to indicate that the server was started successfully and is now listening for incoming requests. This is not what gets called on every new incoming request. The callback passed to .createServer()

is what gets called for every new incoming request.



Multiple incoming requests can be processed at the same time, but due to the single-threaded nature of node.js, only one request actually executes the JS code at once.

But a long running query is usually usually inactive (for example, waiting for database I / O or disk I / O or network I / O), so other queries can be processed and executed during this idle time. This is the asynchronous nature of node.js and why it is important to use asynchronous I / O programming with node.js rather than synchronous I / O processing, as asynchronous I / O allows other requests to run during the time node.js is just waiting for I / O ...

+2


source


Yes, it basically binds an event listener to this port; similar to how event listeners work in your own code. More advanced uses would include sockets, etc.



https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback

+1


source


The other answers are essentially correct, but I would like to add more details.

When you call createServer

, the handler you are passing through is called on every incoming HTTP connection. But it's just setting this up: it doesn't actually start the server or start listening for those connections. This doesn't happen until you name it listen

.

The callback (optional) for listen

is what is called when the server has started successfully and is now listening for connections. In most cases, it just used to log into the console that the server was running on. You can also use it to record server startup times for monitoring uptime. This callback is NOT called for every HTTP request: only once at server startup.

You don't even need to submit a callback for listen

; it works fine without it. Below are some general options (note that it is a good idea to specify the port in an environment variable, usually PORT

if this environment variable is not set, there is a default):

// all in one line, no startup message
var server = http.createServer(handler).listen(process.env.PORT || 8083);

// two lines, no startup message
var server = http.createServer(handler);  // server NOT started
server.listen(process.env.PORT || 8083);  // server started, no confirmation

// most typical variation
var server = http.createServer(handler);
server.listen(process.env.PORT || 8083, function() {
    // server started, startup confirmed - note that this only gets called once
    console.log('server started at ' + Date.now());
});

      

+1


source







All Articles