46

I noticed that almost all examples of Express.js applications use port 3000 as the default listening port for HTTP servers. Is this just because it's a rarely used port, or is there any other reason for this port number?

If I want to run multiple apps side-by-side on my local machine, is it good practice to use ports like 3000, 3001, 3002, etc.?

(I understand that ideally, you'd let the system assign ports. This is just a question as a matter of simplicity, and why 3000 seems to be a conventional assignment.)

M Miller
  • 5,364
  • 9
  • 43
  • 65
  • 1
    The only reason that I can see is that normally in UNIX systems, low ports require root access and high ports don't need to have root access. for example port 80 need root access, port 8080 don't need. I'm not sure if express have any other reason to use 3000 port. – danilodeveloper Jun 20 '16 at 18:18

1 Answers1

56

3000 is a somewhat arbitrary port number chosen because it allows you to experiment with express without root access (elevated privilege). Ports 80 and 443 are the default HTTP and HTTPS ports but they require elevated privilege in most environments.

Using port 3000 in examples also helps indirectly emphasize that you ideally want to put your express app behind nginx or Apache httpd or something like that which would listen on port 80 and/or 443.

There is no reason (that I'm aware of, anyway) why 3000 is better than 8000 or 4000 or 8080 or any of a number of other port numbers that are accessible without elevated privileges.

Trott
  • 66,479
  • 23
  • 173
  • 212
  • 1
    Cool, very helpful. I also noted that the lower 3xxx ports don't have too many major uses so they're pretty safe for development usage. https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers – M Miller Jun 20 '16 at 20:34
  • It's also important to note that ports < 1024 require escalated privilege to run on, further reading here: https://unix.stackexchange.com/questions/16564/why-are-the-first-1024-ports-restricted-to-the-root-user-only – AO_ Sep 06 '17 at 15:13
  • For proxying node.js, I think Apache `httpd` is not as good as `nginx` for performance reasons? – zypA13510 May 11 '18 at 03:07
  • I don't quite understand this part of the answer because nginx doesn't automatically expose port 3000 "Using port 3000 in examples also helps indirectly emphasize that you ideally want to put your express app behind nginx or Apache httpd or something like that which would listen on port 80 and/or 443." – goonerify May 15 '20 at 23:26
  • 1
    @goonerify nginx typically exposes port 80 and/or port 443 and acts as a proxy for access to some backend service that is using some arbitrary port, such as 3000. By having the Node.js example expose an HTTP server on port 3000, it implies (at least to me) that this is not something that is directly exposed to the world. It implies (again, at least to me) that you should put something like nginx in front of it if you want the world to have access to it. – Trott May 16 '20 at 04:39