Skip to content

Assign a Random Port to Node.js Server

View on Twitter

💡 How do you conveniently assign an available port to an app?

👉🏼 Use PORT=0! The kernel will pick a random unused port number and bind it to your app. This is useful when you're deploying multiple apps on a single server.

Use server.listen(0) to let the OS assign a randomly available port to the Node.js server. Print the assigned port with server.address().port.
You might also like
How Big Tech Companies Test Code Read tip
Sites-Available vs Sites-Enabled in Nginx Read tip
Secure File Transfer From/To Remote Server Read tip