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>
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.
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.
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 isapp.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.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:
Static Content
We can serve this up easily as follows:
Same thing if you are using Bower.io:
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.
In the above code we are modifying our Socket.io server to operate on our virtualDirpath and not the default path (
Web.config changes'/socket.io'
is the default path).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.
Then we need to add our url re-writing for the Socket.io paths
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.
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