简体   繁体   English

如何在不同端口上运行Signle Node.js项目

[英]How to run a signle node.js project on different ports

How can I run node.js project on 2 different ports simultaneously? 如何在两个不同的端口上同时运行node.js项目? eg domain.com:1010/ and domain.com:2020/ 例如domain.com:1010/和domain.com:2020/

But the source of these 2 different ports should be the same project. 但是,这两个不同端口的来源应该是同一项目。 Thanks. 谢谢。

If you have your http.Server class in a variable, like so: 如果将http.Server类包含在变量中,则如下所示:

var server = http.createServer(handler);

you cannot call .listen() multiple times; 您不能多次调用.listen() the subsequent calls have no effect. 随后的调用无效。

But what you can do is register two http.Server classes with the same handler and have the .listen() to different ports, like: 但是您可以做的是使用相同的处理程序注册两个http.Server类,并将.listen()分配到不同的端口,例如:

var server1 = http.createServer(handler)
var server2 = http.createServer(handler)

server1.listen(3000);
server2.listen(5000);

The first idea that came to my head is passing port as argument. 我想到的第一个想法是通过端口作为参数。 With that there should be no problem to use 这样就没有问题了

$ node app.js 1010

in one terminal and 在一个终端中

$ node app.js 2020

in another. 在另一个。 You can get arguments passed to your program with an array 您可以使用数组将参数传递给程序

process.argv

for example process.argv[1] should give you port if you start your app as I did upper 例如process.argv[1]应该在您启动应用程序时为您提供端口,就像我在上面做的那样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM