How does node deal with the problem that too many ports cause too many processes to open and consume too much memory?

  node.js, question

Platform and Use Plug-ins

node pm2 express

Problem description:

A server will open many websites with different interfaces and the same functions (they use the same interface, but the back-end port is different from the front-end access port. )。 The implementation method on my side is to use pm2 to start different websites, and the background port and the foreground access port will have different configuration files. The startup screenshot is as follows:


It is no problem to start several in this way, but if you start 10 or 20, there will be insufficient memory.

I think the solution is:

Because they have the same content except for different interfaces, what I want is to start a copy of common content, and then different ports come in to display different interfaces and obtain corresponding configurations. How to implement the specific code or what plug-in does node have? I heard from the back end that tomcat has similar functions. How can it be implemented on node?

Solution codes and tips found online

Each HTTP server can only listen to one port at a time. Also, this is super easy to do with Express. – Brad Oct 10 ’13 at 13:18

var http=require('http');
 var ports = [7006, 7007, 7008, 7009];
 var servers = [];
 var s;
 function reqHandler(req, res) {
 remoteAddress: req.socket.remoteAddress,
 remotePort: req.socket.remotePort,
 localAddress: req.socket.localAddress,
 localPort: req.socket.localPort,
 ports.forEach(function(port) {
 s = http.createServer(reqHandler);

What is the good solution, or people with relevant actual combat experience hope to give me some advice and guidance, thank you.

If you use pm2 management, can you limit the memory