简体   繁体   中英

Node.js + Express only work on port 3000 on test server

On Local Test it work. on different port (3001, 8080)

But on Test Server (Azure)

I run 2 instance of Node App on same machine

$ node api1/index.js (on port 3000)
$ node api2/index.js (on port 3001)

and

$ node api1/index.js (on port 3001)
$ node api2/index.js (on port 3000)

But it only works on port 3000.

How do I set different port in Express?

Now, I've changed at app.listen(3001) on index.js and it doesn't work.

Often cloud platforms set an environment variable that contains the port they want you to stick your app on. I don't have any experience with Azure... See the answer here: How to run a node.js server on Azure?

Specifically:

var port = process.env.port

Most cloud providers in my experience don't let you play on other ports. You can always specify a localhost port too, though by doing this:

var port = process.env.port || 3001 //(or whatever)

app.listen(port);

this way if process.env.port is undefined (which it will be in your dev environment) you fallback to 3001.

Make sense?

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