5

I have my website working within express. But now I need to host it on IIS. How can I get my routing to work with IISNode?

I was able to move my static content to a /public folder (html, front end js, etc). I didn't want to do this, but got that part working.

I moved my server side logic to /server.

I had previously had .Net Web API style routing working, where my services were hosted at /api. I moved these to /server/api.

Whenever I try to request my API, I get a 404. How can I get this working with IISNode?

app.use("/api", require("./api"));

// routes/api/index.js
var router = require('express').Router();
router.use('/questionsets', require('./questionsets'));
module.exports = router;   

var router = require('express').Router();
router.use('/questionsets', require('./questionsets.js'));
module.exports = router;

// routes/api/questions.js
var router = require('express').Router();

router.get('/', function (req, res) {
    ...
});

router.get('/:id', function (req, res) {
    ...
});

module.exports = router;        


<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin" />
        </hiddenSegments>
      </requestFiltering>
    </security>
    <handlers>
      <add name="iisnode" path="bin/www" verb="*" modules="iisnode" />
    </handlers>
    <iisnode loggingEnabled="false" />
    <httpErrors errorMode="Detailed" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" prefixLanguageFilePath="" path="/" responseMode="ExecuteURL" />
    </httpErrors>
    <rewrite>
      <rules>
        <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="iisnode" />
        </rule>
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^bin\/www\/debug[\/]?" />
        </rule>
        <rule name="StaticContent" patternSyntax="ECMAScript" stopProcessing="true">
          <match url=".*" />
          <action type="Rewrite" url="{C:1}" logRewrittenUrl="true" />
          <conditions>
            <add input="{REQUEST_URI}" pattern=".*?\/(.*)" />
          </conditions>
        </rule>
        <rule name="DynamicContent" patternSyntax="ECMAScript">
          <match url=".*" />
          <conditions>
            <add input="{{REQUEST_FILENAME}}" matchType="IsFile" negate="True" />
          </conditions>
          <action type="Rewrite" url="server/app.js" logRewrittenUrl="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Source for url rewriting configuration

Hoppe
  • 6,508
  • 17
  • 60
  • 114

1 Answers1

0

You need to install Application Request Routing here , and the documentation here.

Simply putting your server side files under /server would not work. Start your express server, let's say listenning to http://localhost:3200/ , route request '/' to 'localhost:3200' will just route each request to your express server.

YLS
  • 697
  • 5
  • 9
  • Shouldn't I open the browser to the IIS Port - not the express port? And do I need to start Express, or will IIS start it for me? – Hoppe Nov 10 '16 at 14:12
  • Actually you need to start both. Express itself is a web server just link IIS. Opening IIS, means it start to accept request and route those request to express. So there has to be an express already running. In this case, open the browser to the IIS port (eg. http://a.b.c:80), and configure IIS so that it route requests to express port, like 'localhost:3000'. – YLS Nov 12 '16 at 06:47