IIS, Node.js, and Web Application with IISNode not configured right with Virtual Directory
Asked Answered
C

3

17

I have the following setup in IIS:

  • Default Web Site (www.foo.com) hosting standard html site
  • Web Application underneath Default Web Site (www.foo.com/bar) running IIS Node
  • Node project is utilizing express

I cannot for the life of me get this thing configure correctly so when I hit the web application is serves up the node application correctly. I think my problem lies in the web.config. Can anybody help me write a correct web.config to get this working correctly? The current version of my config will server me a node response that says it cannot get the resource at whatever url I type.

Here is the current version of my config:

<configuration>
  <system.webServer>    
    <handlers>
      <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>    
    <rewrite>
      <rules>
        <rule name="bar">
          <match url="bar/*" />
          <action type="Rewrite" url="app.js" />
        </rule>
      </rules>
    </rewrite>    
  </system.webServer>
</configuration>
Custodian answered 12/4, 2013 at 20:0 Comment(3)
I've found it easier (for my local development purpose) to just create dedicated web site for node.js app (as recommended in answer to similar question) rather then juggling with web application settings under Default Web Site.Slumberous
Out of curiosity have you tried using (or are you using) NodeJSTools for Visual Studio?Peace
Have you definately installed the iis url rewrite module?Galactic
R
27

I ran into the same issue a while back, running my app in a Virtual Directory.

After lots of time wasted and struggling I was able to put all the pieces together to get my apps to work in a Virtual Directory, this included apps using Socket.io

Since there isn't much documentation out there for this particular scenario and the resources that are available, that I've found, only partially described how to solve this issue. Here is a tutorial on how to get all of this working. I personally have multiple Node.js web services implementing either a REST API or Socket.io using this setup.

I strongly recommend using the Web.config template below to get this working.

IISNode Web.config Template

https://gist.github.com/pbaio/f63918181d8d7f8ee1d2

The config in the above link has some comments I put in there to help with ease of use. Its configured to use app.js as the main file but if your file is named something different simply switch the value to use that file instead.

To get this config working you will need the URL Re-write Module for IIS if you don't already have it installed.

Default Setup

By default this template is setup to work in a standard Web App running in IIS and not in Virtual directory environment. However, with some minor tweaking you can use this same Web.config to run a Node.js app in a Virtual Directory.

Get Express to use your Virtual Directory

IISNode makes all keys declared in your <appSettings> environment variables. We can use this to our advantage to setup our Virtual Directory path and expose it to our main file. In the template above our main file is app.js.

Get our Virtual Directory Path

We need to get the path that our application will be routed from in our Web.config file. We do this by accessing our environment variables on our process object. Add the following line to our app.js file.

var virtualDirPath = process.env.virtualDirPath || '';

This retrieves our virtualDirPath from our Web.config and give it a default value of empty string.

Routing Pages

Then we can prepend the virtualDirPath to our routes and if you are using a view engine such as Jade or EJS we can pass our Virtual Directory path for hyperlinks and such to the view:

var app = require('express')();
app.get(virtualDirPath + '/', function(req, res) {
  res.render('index', { virtualDirPath: virtualDirPath });
});

Static Content

We can serve this up easily as follows:

app.use(express.static(path.join(virtualDirPath, 'public')));

Same thing if you are using Bower.io:

app.use('/bower_components', express.static(path.join(virtualDirPath,'bower_components')));

Using Virtual Directories with Express & Socket.io

When using Virtual Directories with Socket.io we need to make changes to the configuration for both the Server and the Client.

Server-Side

We need to configure our Socket.io Server slightly different than you normally would.

var app = require('express')();

var virtualDirPath = process.env.virtualDirPath || '';

var server = require('http').Server(app);
var io = require('socket.io')(server, { path: virtualDirPath + '/socket.io' });
// Get the port that we should be listening on
server.listen(process.env.PORT || 8080);

In the above code we are modifying our Socket.io server to operate on our virtualDirpath and not the default path ('/socket.io' is the default path).

Web.config changes

In order for IISNode to properly work with socket.io we also need to add some additional url re-writing and swap out our handler. Within the template config file from above we can see the Socket.io handler on line 57, it is commented out in the template.

<add name="iisnode-socket.io" path="app.js" verb="*" modules="iisnode" />

Then we need to add our url re-writing for the Socket.io paths

<rule name="SocketIO" patternSyntax="ECMAScript">
    <match url="socket.io.+" />
    <action type="Rewrite" url="app.js"/>
</rule>

Client-Side

On the Client-Side we just need to specify the path that the Socket.io server is listening at instead of its default path.

var socket = io.connect('http://example.com:port', { path: '/virtualDirPath/socket.io' });

Everything should be good to go at this point with your Socket.io application running in a Virtual Directory with IISNode.

Environment Info

The apps that use this config were built with Node.js, Express 4.12.3 and running in IIS 7.5 with IISNode installed. Additionally, by changing the handler in the conifg file, Socket.io can be used in a Virtual Directory as well. The Socket.io version used in the above example was 1.3.5

Radar answered 17/7, 2015 at 18:17 Comment(2)
Thanks for the answer, definitely one of the most complete ones I've seen! Just a pity that the virtualDirPath has to be everywhere (routes, redirects, views (css, images)) and therefor the app can't be independent of it as point out here.Sabadilla
Excellent answer, Everyone! take care of CORS and correct client version for socket.io server.Pointblank
I
0

I ran into this same problem.

Ultimately, what worked was fixing the request url in my application. We've switched from using express to hapi, so I've converted this code from hapi back to express without testing it first, but it should get you in the ballpark.

app.use(function (req, res, next) {
    if(!settings.serverPath) return next();

    console.log(req.url);
    var regex = new RegExp('(' + settings.serverPath + ')(/.+)', "i");
    req.url = req.url.replace(regex, "$2");
    console.log(req.url);
    return reply.continue();
});

//set up your routes here...
Insurance answered 20/2, 2015 at 14:17 Comment(0)
T
0

For those who are not able to serve static files be sure to change your base href url to virtual directory path before doing ng build and then from server serve your static files.Hope this helps someone.

Transit answered 2/4, 2018 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.