简体   繁体   English

node js自动更改端口

[英]node js automatically change port

I am using node Js, and I want to know if there is a way to make my nodeJs automatically change the PORT if the default one has been used.我正在使用节点 Js,我想知道是否有办法让我的 nodeJs 在使用默认端口时自动更改端口。

for exemple: the default port for my server to run on is 3000. But if that port is already in use, it should maybe ask me if I want to switch port and then look up the first unused port to run on.例如:我的服务器运行的默认端口是 3000。但如果该端口已被使用,它可能会询问我是否要切换端口,然后查找第一个未使用的端口以运行。

Thank you谢谢

You can use this library to find a free port that is not being used incrementally.您可以使用此库来查找未增量使用的空闲端口。 (There is still a slight chance for race condition a port might be used after the code is run, to mitigate that wrap it in a try catch block and call find-free-port again). (在代码运行后仍然有可能使用端口的竞争条件,以减轻将其包装在 try catch 块中并再次调用find-free-port情况)。

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

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