Kode
Kode

Reputation: 3215

express.Router not working in IIS hosted node app

When running my Node/Express app locally (ex. npm start), my routes load without issue. However, when the app is hosted in IIS (have tried iisnode, reverse proxy, and httpplaformhandler) it gives a 404 on the routes... specifically CANNOT GET.

These routes reside in a "routes" directory.

Here is my solution structure:

node_modules
public
    client.html
routes
    api1.js
    api2.js
server.js
web.config

Here is my server.js where the routes are loaded:

// 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 (currently using httpplatformhandler):

<?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>
  1. The / loads the client.html page without issue as you pull up the root domain
  2. The /api-docs loads Swagger without issue
  3. The /api1 fails with cannot get/404
  4. The /api2 fails with cannot get/404

Since this is IIS, I have tried a fuller "path" to routes. Ex. routes/api1 but that fails to work.

Does Express.Router not work with an node/express app hosted in IIS?

When I set this up as an Reverse Proxy the localhost:3000 runs the /api1 without issue but the domain in IIS of mynode.com throws a Cannot Get /api1... even though it is supposed to just be a proxy.

Failed Request Tracing shows the following:

MODULE_SET_RESPONSE_ERROR_STATUS

ModuleName httpPlatformHandler

Notification EXECUTE_REQUEST_HANDLER

HttpStatus 404

HttpReason Not Found

HttpSubStatus 0

ErrorCode The operation completed successfully. (0x0)

Upvotes: 0

Views: 106

Answers (0)

Related Questions