How to run a node.js app in an azure website subdirectory

1.5k Views Asked by At

I have already created a website using Azure and I would like to run a node.js app in a subdirectory ( ex. app1/test) rather then creating a new website and running the app in the root directory.

Here is the structure of my directories

-wwwroot/web.config

-wwwroot/index.html

-wwwroot/app1/test/package.json

-wwwroot/app1/test/server.js

So there is an index.html in the root and then I have the server.js in test subdirectory.

This is my server.js:

var http = require('http');
var port = process.env.port || 1337;
http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World\n');
}).listen(port);

and this is my package.json:

{
  "name": "test",
  "version": "0.0.0",
  "description": "test",
  "main": "server.js",
  "author": {
    "name": "name",
    "email": ""
  },
  "dependencies": {
  }
}

my web.config:

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <location path="app1/test">
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="false" />
    <!-- indicates that the server.js file is a node.js application 
    to be handled by the iisnode module -->
    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="app" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
            <match url="iisnode.+" negate="true" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
            <action type="Rewrite" url="server.js" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>  
  </location>
    <system.webServer>
     <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
    </handlers>
    </system.webServer>
  </configuration>

But I get

"The page cannot be displayed because an internal server error has occurred."

when i hit the domain-name/app1/test

Not sure what is wrong with my web.config and if i need to manually install iisnode.

I appreciate any help.

Thanks

2

There are 2 best solutions below

3
On

You need to check if your request url is at the location you're checking, in this case:

var http = require('http');
var port = process.env.port || 1337;
http.createServer(function (req, res) {
  if(req.url === '/app1/test')
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World\n');
}).listen(port);

and then you can test it by going to page

http://localhost:1337/app1/test
0
On

I solved it! The following works!

<match url="app1/test" />
<action type="Rewrite" url="app1/test/server.js" />