user3616544
user3616544

Reputation: 1073

Node.js and Express application running on Azure App Service Linux using PM2 only works on port 8080

I am trying to use PM2 on Azure App Service Linux running a simple Node.js/Express application. The configuration file for PM2 is like this:

module.exports = {
  apps: [
    {
      name: "myapi",
      script: "./src/index.js",
      env: {
        NODE_ENV: "development",
        PORT: 3000,
      },
      env_test: {
        NODE_ENV: "test",
        PORT: 8081,
      },
      env_production: {
        NODE_ENV: "production",
        PORT: 8080,
      }
    }
  ]
};

The startup command is like

pm2-runtime start ecosystem.config.js --env test

The deployment is fine. But I can only use PORT = 8080. If I use port 8081, I got the 504 gateway timeout error.

What might be the problem?

Upvotes: 0

Views: 157

Answers (1)

Aslesha Kantamsetti
Aslesha Kantamsetti

Reputation: 1516

By default, Azure Web Apps only allow external access on ports 80 (HTTP) and 443 (HTTPS). On Azure Linux Web Apps, containers typically run on port 8080.

It is not possible to expose multiple ports for external access in an Azure Web App.

I created a sample Node.js Express app with PM2 and deployed it to Azure App Service (Linux).

The app should be configured to listen on the port specified by process.env.PORT for compatibility with Azure’s port routing.

Below is complete code of nodejs app.

ecosystem.config.js:

module.exports = {
    apps: [
      {
        name: "myapi",
        script: "./src/index.js",
        env: {
          NODE_ENV: "development",
          PORT: 3000, 
        },
        env_test: {
          NODE_ENV: "test",
          PORT: process.env.PORT || 8080, 
        },
        env_production: {
          NODE_ENV: "production",
          PORT: process.env.PORT || 8080, 
        }
      }
    ]
  }; 

index.js:

const express = require('express');
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World!');
});
const port = process.env.PORT || 8080;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

I used the below start script in package.json :

"scripts": {
"start": "pm2-runtime start ecosystem.config.js"
},

I set the Startup Command as shown below.

enter image description here

enter image description here

Azure Output :

enter image description here

Upvotes: 0

Related Questions