Press "Enter" to skip to content

Serving Node Applications on Microsoft IIS

Over the weekend I was bored and tried hosting node applications on IIS on a server. Since currently I have only tried wordpress I had no idea how to go about this which might be why you are here, so lets dive straight into how to get it running. So first of all if you haven’t done this before you will probably not have node installed on your server. So first lets install everything that we will need.

Prerequisites

  1. If you do not have IIS (Internet Information Services) enabled; you will have to enable it.
    Search Turn Windows Features On or Off and check the box next to Internet Information Services.  Expand the Internet Information Services node to select additional features like Web Management Tools and World Wide Web Services. Ensure at least the following are selected:

    • Web Management Tools: Includes IIS Management Console.
    • World Wide Web Services: Includes various services for hosting websites and applications.
  2. Install node from here. Make sure that you add the path to node.exe in your environment variables. In most cases it would be C:\Program Files\nodejs\.
  3. Install iisnode from here.
  4. Install URL Rewrite from here.

Creating and Setting up the Site

  1. Open IIS Manager and click on your device in the connections panel on the left to expand it.
  2. Right click Sites and click on the Add Website... option to create a new site, and add the domain name you want it to show up on in the hostname and check the start website immediately.
  3. Add 127.0.0.1 to your Site from the bindings in the right pane in your site’s page in IIS.
  4. Check if iisnode is installed in modules.
  5. Locate the folder C:\intepub\wwwroot\ and create a folder with the same name as your site.
  6. Add the following files to C:\inetpup\wwwroot\{site name}:
    • server.js
    • web.config
  7. Open the terminal and install ExpressJs using npm install express.
  8. Write your server script in server.js to create an express server.
    var express = require("express");
    var app = express();
    
    app.get("/ping", function(req, res) {
      res.send("Pong!");
    });
    
    app.listen(process.env.PORT, () => {
      console.log("listening");
    });
  9. Add the following to web.config

    <configuration> 
        <system.webServer>
      
         <handlers>
           <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
         </handlers>
      
         <rewrite>
           <rules>
             <rule name="nodejs">
               <match url="(.*)" />
               <conditions>
                 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
               </conditions>
               <action type="Rewrite" url="/node_app.js" />
             </rule>
           </rules>
         </rewrite>
      
         <security>
           <requestFiltering>
             <hiddenSegments>
               <add segment="node_modules" />
               <add segment="iisnode" />
             </hiddenSegments>
           </requestFiltering>
         </security>
         <httpErrors existingResponse="PassThrough" />
         <iisnode nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" />
         </system.webServer> 
     </configuration>

    This web.config file is written in XML and is used to configure settings for web applications hosted on Internet Information Services (IIS). It allows you to define how IIS should handle requests, manage security settings, rewrite URLs, handle custom error messages, and integrate with specific modules like iisnode for hosting Node.js applications.

Note: If you are having trouble creating or editing files in C:\inetput\wwwroot\{your site} , right click and open properties. Add your user if it does not exist and check the full control box to make the process of editing and creating files easier. In most cases this requires administrative control.

And with that we have served node applications on IIS!

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *