Hosting node.js apps in IIS on Windows
Prerequisites
- Windows Vista, 7, 8, 10 or Windows Server 2008 or greater
- IIS 7.x or greater with IIS Management Tools and ASP.NET
- WebSocket functionality requires IIS 8.x on Windows 8 or Windows Server 2012
- URL rewrite module for IIS
- Latest node.js build for Windows
- iisnode
Description
This post walks thourgh a simple example of how to run node.js applications that use the popular express framework in IIS.
Install iisnode
Install the following first on the IIS server you are using to host the application.
- NodeJS (latest version)
- iisnode x64
○ https://github.com/Azure/iisnode - Download URL Rewrite extension for IIS (needed for express apps)
○ https://www.iis.net/downloads/microsoft/url-rewrite
NOTE: After installation of iisnode, if you setup the samples (by running setupsamples.bat in issnode folder) and you get this error in the browser
500.19 webconfig error
That error is fixed by allowing the Handler to Read/Write. On IIS go to:
Step 1:
In IIS Manager click on the server on the left side.
Select Feature Delegation
Click Custom Site Delegation (on very right set of navigation items)
Select Handler Mappings
and set its value to Read/Write
Step 2:
Restart IIS server
Create your node.js application
This is a simple web api that has a single GET method that returns the word ‘hello’. Create a folder for your application, in this case it is called helloexpress
and from a terminal window navigate to that folder and run the following commands
Use defaults for all except entry point - make sure this matches with the name of the javascript file you want to use as the entry point. In this case we are calling it server.js
.
npm init
Once initialization is complete, you should have a package.json
file in your application folder.
From the terminal run the following to install the ExpressJS package
npm install express
Create a file called server.ja
in your application folder with the following code.
const Express = require('express');
const app = Express();
app.get('/helloexpress', (req, res)=>{
res.send("hello")
})
app.listen(process.env.PORT)
NOTE: You cannot assign your own port number, so make sure in your app you listen on the following port
process.env.PORT
This represents an environment variable on the machine called PORT. It is configured for you by IIS and we do not know ahead of time what value it will be.
Add a file called web.config
with the following contents - make sure to specify the path to the server.js file
<configuration>
<system.webServer>
<!-- 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>
<!-- use URL rewriting to redirect the entire branch of the URL namespace
to server.js node.js application; for example, the following URLs will
all be handled by hello.js:
http://localhost/node/express/myapp/foo
http://localhost/node/express/myapp/bar
-->
<rewrite>
<rules>
<rule name="helloexpress">
<match url="/*" />
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
<!-- exclude node_modules directory and subdirectories from serving
by IIS since these are implementation details of node.js applications -->
<security>
<requestFiltering>
<hiddenSegments>
<add segment="node_modules" />
</hiddenSegments>
</requestFiltering>
</security>
</system.webServer>
</configuration>
Once complete your folder structure should look like this
helloexpress
│ - package-lock.json
│ - package.json
│ - server.js
│ - web.config
│
└───node_modules
Copy your folder somewhere, it can be anywhere, I put mine in the default iis websites folder.
Then create a new application under Default Web Site and point to the physical path of the application above, (you can also expose the app in several other ways, for example by creating another website).
NOTE: Don’t forget to give IIS_IUSER full read/write permission on the application folder.
When finished, open a browser window and navigate to the following URL
http://localhost/helloexpress
You should see thw following output.
Sample Code
https://github.com/erotavlas/blog-samples/tree/master/Hosting-node-js-apps-in-IIS-on-Windows
References
Salvatore S. © 2020