Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does server.listen() actually do?

Disclaimer: Über-noob.

This question may have more to do with how files are executed than what http or node do. I have the following simple-as-all-hell file that will be executed with node to make a local server.

const http = require("http");

const hostname = "127.0.0.1";
const port = 8000;

// Create http server
const server = http.createServer( function (req,res)  {
  // set response http header with http status and content type
  res.writeHead(200, {"Content-Type": "text/plain"});

  // send response "hello world"
  res.end("G'day mate\n");
});


// Listen on a given port and print a log when it starts listening

server.listen(port, hostname, () => {
  console.log("heyy maattteeeyyy")
});

My question: Why, when I enter the command node hello.js does this file keep executing / keep running? I presume it is something about the final server.listen() command that says "when you run this, just keep running, I am now hosting something", but what exactly is that?

Edit: I would greatly appreciate detailed and exact explanations -- I am just learning, but the purpose of the question is to understand exactly what is going on.

Thank you

like image 507
machine gurning Avatar asked Jan 24 '26 23:01

machine gurning


2 Answers

It starts a TCP server listening for requests, waiting to react on them.

To understand how this matters, you have to know that node won't terminate simply when code execution reached the end of the input file, it ends when node's event loop is "empty" (as it's often referred to), or in more precise terminology, no longer "alive".

Check out the libuv docs (libuv is what node.js uses for I/O events and its event loop).

It says the following about the phase of the loop in which a decision is made whether it should continue running:

  1. If the loop is alive an iteration is started, otherwise the loop will exit immediately. So, when is a loop considered to be alive? If a loop has active and ref’d handles, active requests or closing handles it’s considered to be alive.

And this is the explanation to what a "handle" is:

libuv provides users with 2 abstractions to work with, in combination with the event loop: handles and requests.

Handles represent long-lived objects capable of performing certain operations while active. Some examples:

  • A prepare handle gets its callback called once every loop iteration when active.
  • A TCP server handle that gets its connection callback called every time there is a new connection.

Requests represent (typically) short-lived operations. These operations can be performed over a handle: write requests are used to write data on a handle; or standalone: getaddrinfo requests don’t need a handle they run directly on the loop.

(Emphasis mine.)

So, the fact that server.listen will listen on a TCP port and attach an I/O callback to it to react on incoming connections means that there is now a referenced handle in the loop, which keeps the loop alive and therefore the process won't exit.

You may notice that the libuv docs I linked don't say anything about the microtask queue and other more JavaScript-related and more well-known parts of what's considered "the event loop". That's because they are handled by V8, and how these things play into each other is explained in more detail in this article if you are interested.

like image 123
CherryDT Avatar answered Jan 26 '26 14:01

CherryDT


The short answer - it will start the listening on a port and sending that traffic to this process and it will also keep the nodejs process for automatically exiting as long as the server is running

The long answer is nodejs docs: https://nodejs.org/api/net.html#serverlisten

like image 30
Andrey Bessonov Avatar answered Jan 26 '26 15:01

Andrey Bessonov