Skip to content

The registration order of http server listen depends on the Node.js version #53204

Description

@yamachu

Version

v20.13.1

Platform

Darwin 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:16:51 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8103 arm64

Subsystem

No response

What steps will reproduce the bug?

The behavior of Server.listen on the same port has changed between node 20.12 and 20.13.

If you execute code like the following,
In node 20.12, 127.0.0.1:8888 (or 0.0.0.0:8888, localhost:8888) is listened to and the Hello World string is returned.
However, in node 20.13, only 192.168.10.104:8888 is listened to, and the rest, such as 127.0.0.1:8888, are not listened to.

import { createServer } from "node:http";

const server = createServer((_, res) => {
  res.writeHead(200, { "Content-Type": "text/plain" });
  res.end("Hello World\n");
});

// listen order dependents on the version of node.js...
server.listen(8888, "127.0.0.1"); // 20.13 < , this one is used.
server.listen(8888, "192.168.10.104" /* replace your local ip address */); // 20.13 >= , this one is used.

How often does it reproduce? Is there a required condition?

always

What is the expected behavior? Why is that the expected behavior?

Both will be LISTENED to, or if there is a specification for the order, that document will be clearly noted.

What do you see instead?

no outputs

Additional information

For example, this is how it is used.
https://github.com/Azure/static-web-apps-cli/blob/352be8f4ce2d01e1dac17797a80f9f414d612bc0/src/msha/server.ts#L173-L174

Activity

  1. added
    httpIssues and PRs related to the http subsystem.
    on May 29, 2024
  2. avivkeller commented on May 29, 2024

    @avivkeller
    Member

    @nodejs/http

  3. avivkeller commented on May 29, 2024

    @avivkeller
    Member

    Reference: Azure/static-web-apps-cli#829

    Looking at the changelogs between 20.12.2 and 20.13.0, I don't see anything that directly says it changed this functionality, but the following commit might be of interest: #52492 . I'm not sure, but it's involving DNS ordering, so it might be worth a look.

    Changelog: https://nodejs.org/en/blog/release/v20.13.0

  4. theanarkh commented on May 30, 2024

    @theanarkh
    Contributor

    I think this is related to this, this PR makes the last listen is valid. Before this PR, the follow code just listen to 127.0.0.1(I think that's not right it should listen to 192.168.10.104) instead of both. Maybe we should update the docs.

    server.listen(8888, "127.0.0.1"); // 20.13 < , this one is used.
    server.listen(8888, "192.168.10.104" /* replace your local ip address */); // 20.13 >= , this one is used.
  5. avivkeller commented on May 30, 2024

    @avivkeller
    Member

    I think this is related to this. this PR makes the last listen is valid. Before this PR, the follow code just listen to 127.0.0.1 instead of both. I think that's not right, maybe we should update the docs.

    server.listen(8888, "127.0.0.1"); // 20.13 < , this one is used.
    
    server.listen(8888, "192.168.10.104" /* replace your local ip address */); // 20.13 >= , this one is used.

    Ahh that makes more sense

  6. mcollina commented on May 30, 2024

    @mcollina
    SponsorMember

    I didn't even know multiple .listen() was possible. I think we should just error in this case, or add support for multiple interfaces. In Fastify, we handle it with having multiple server instances.

  7. github-actions commented on Jul 31, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 90 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  8. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 31, 2026
  9. brynary commented on Aug 30, 2026

    @brynary

    This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 120 days).
    If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.

  10. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpIssues and PRs related to the http subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions