HttpPlatformHandler Returns Random Port but Node is Set to 3000

67 Views Asked by At

I have a Node App that runs on Port 3000 when started and all routes work (ex. NPM Start).

I am attempting to host this App in IIS via the HttpPlatformHandler but each time it starts it uses a random port and the routes don't work (besides the main / html page and /api-docs for swagger-ui), despite the port being stated in the server.js of my app (and starting properly on port 3000 when started from PowerShell with an NPM Start command).

Why is a random port being chosen (I believe this is why my routes are not found).

Here is my server.js:

// MODULES AND REQUIRES
const express = require("express");
const app = express();
const path = require('path');
const swaggerJsDoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const objectMapper = require('object-mapper');
const cors = require('cors');

// Require Routes
var api1 = require('./routes/api1.js')
var api2 = require('./routes/api2.js')

// PORTS AND BASIC EXPRESS APP SETTINGS
const port = process.env.PORT || 3000;

// CORS ALLOW ALL. NOTE IP RESTRICTIONS ARE IN PLACE
app.use(cors({
  origin: '*'
}));

// ignore request for FavIcon. so there is no error in browser
const ignoreFavicon = (req, res, next) => {
  if (req.originalUrl.includes('favicon.ico')) {
      res.status(204).end();
  }
  next();
};

// Configure nonFeature
app.use(ignoreFavicon);

// Root Route - Serve Static File
app.get('/', (req, res) => {
      res.sendFile(path.join(__dirname, '/public/client.html'));
});

// SWAGGER UI CONFIGURATION

// Primary Swagger Options
const options = {
  customCss: '.swagger-ui .topbar { display: none } .swagger-ui .scheme-container { display: none }'
};

// Custom Swagger Options: https://swagger.io/specification/#infoObject
const swaggerOptions = {
  swaggerDefinition: {
    info: {
      version: "2.0.0",
      title: "My App",
      description: "This page lists the available APIs within my app and allows you to test them.",
      contact: {
        name: "My Name"
      },
      servers: [{"url":"http://localhost:3000", "description": "Development server"}]
    }
  },
  // ['.routes/*.js'] Location for APIs
  apis: ["./routes/*.js"],
};

const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs, options));


// ROUTES
  app.use('/api1', api1)
  app.use('/api2', api2)  

// APP LISTEN WITH SSL/HTTPS
app.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});

Here is my Web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="httppPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
    </handlers>
    <httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\logs\node.log" startupTimeLimit="20" processPath="C:\Program Files\nodejs\node.exe" arguments=".\server.js">
            <environmentVariables>
                <environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
                <environmentVariable name="NODE_ENV" value="Production" />
            </environmentVariables>
        </httpPlatform>
  </system.webServer>
</configuration>
0

There are 0 best solutions below