javascript - What does server.listen() actually do? - Stack Overflow

Disclaimer: Über-noob.This question may have more to do with how files are executed than what http or

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 mand node hello.js does this file keep executing / keep running? I presume it is something about the final server.listen() mand 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

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 mand node hello.js does this file keep executing / keep running? I presume it is something about the final server.listen() mand 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

Share Improve this question edited Oct 22, 2022 at 9:46 machine gurning asked Oct 22, 2022 at 5:52 machine gurningmachine gurning 1174 silver badges9 bronze badges 2
  • Good question. I don't know about you but I was always afraid of reading code. Dig into the function and read the code and it wont be much different from what you already i promise – Mustafa Commented Oct 22, 2022 at 9:54
  • 1 Except it will be C++ after a certain point. – CherryDT Commented Oct 22, 2022 at 14:16
Add a ment  | 

2 Answers 2

Reset to default 5

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 bination 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 ining 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.

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/api/net.html#serverlisten

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744392349a4571971.html

相关推荐

  • javascript - What does server.listen() actually do? - Stack Overflow

    Disclaimer: Über-noob.This question may have more to do with how files are executed than what http or

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信