1
http.createServer(onRequest).listen(8888);
http.createServer(onRequest).listen(8080);

In this case, I understand that two different http servers are created which listen to at different ports.

http.createServer(onRequestA).listen(8888);
http.createServer(onRequestB).listen(8080);

In this case the servers listen on various ports and also do different actions.

I have a a few questions.

  1. Are these two approaches commonly used in the real world?
  2. Is there really an advantage of snippet 1?
  3. If such multiple servers can be created, what is the maximum number of servers that can be created from a single node instance?
4
  • The answer to question number 2 is: It depends. Do you need the same action to be done for both ports? Commented Jul 9, 2018 at 6:17
  • What are you trying to archive by creating multiple ports? Commented Jul 9, 2018 at 6:19
  • As for your third question, there are a maximum of 65536 ports (numbered 0 to 65535). The numbers below 1024 are reserved. Commented Jul 9, 2018 at 6:19
  • @antzshrek Nothing much really. I am learning node.js and practicing a few examples. Was just wondering if multiple http servers can actually be created on various ports, processing the same/different requests etc. Commented Jul 9, 2018 at 6:22

1 Answer 1

2

In answering your questions directly,

  1. Are these two approaches commonly used in the real world?
  • It depends on what you're trying to archive on those ports.
  1. Is there really an advantage of snippet 1?
  • It also depends on the action you plan to take on the ports, if you wanna run the same requests, it doesn't make sense running multiple ports.
  1. If such multiple servers can be created, what is the maximum number of servers that can be created from a single node instance?
  • You might want to NOTE: Standard practices say no non-root process gets to talk to the Internet on a port less than 1024, and also remember that the maximum number you can go for the ports is 65536 i.e (0 ~ 65535).
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.