简体   繁体   中英

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.

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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