NodeJs

Hosting a Node.js API on an IISNode Server

10 min read

  • June 19,2024
  • Yash Garg

Hosting a Node.js API on an IISNode server can be an excellent choice for developers who want to leverage the features of both Node.js and Microsoft’s IIS (Internet Information Services). IISNode enables you to run Node.js applications within the IIS framework, allowing for smooth integration with existing Microsoft infrastructure and tools. This blog will guide you through the steps to successfully host a Node.js API on an IISNode server.

Why Host a Node.js API on IISNode?

Hosting a Node.js API on IISNode offers several benefits:

  1. Windows Integration:Seamless integration with Windows-based environments and Active Directory.
  2. Scalability:Utilizes IIS’s robust infrastructure for load balancing and scalability.
  3. Security:Leverages IIS’s security features, such as SSL/TLS, IP restrictions, and authentication mechanisms.
  4. Management Tools:Access to IIS’s powerful management tools for monitoring and configuring your applications.

Prerequisites

Before you start, make sure you have:

  1. A Windows Server:Ensure you have administrative access.
  2. Node.js Installed:Download and install Node.js from nodejs.org.
  3. IIS Installed:Install IIS on your Windows server if it’s not already installed.
  4. IISNode Installed:Download and install IISNode from iisnode.codeplex.com.

Step-by-Step Guide

1. Install IIS and IISNode

Installing IIS

  1. Open Server Manager.
  2. Click on Add roles and features.
  3. Follow the wizard and select Web Server (IIS).
  4. Ensure you install the Application Development feature set, including ASP.NET and WebSocket Protocol.

Installing IISNode

  1. Download the IISNode installer.
  2. Run the installer and follow the instructions.

2. Set Up Your Node.js Application

  1. Create a directory for your Node.js application on your server, for example, C:\inetpub\wwwroot\nodeapp.
  2. Copy your Node.js API code to this directory.
  3. Ensure your application has an app.js (or similar entry file) that starts your Node.js server.

3. Configure IIS to Host Your Node.js Application

Creating a Site in IIS

  1. Open IIS Manager.
  2. Right-click on Sites and select Add Website.
  3. Enter the site name, physical path (e.g., C:\inetpub\wwwroot\nodeapp), and port.
  4. Click OK to create the site.

Configuring the Application

  1. In IIS Manager, select your newly created site.
  2. Open the Handler Mappings feature.
  3. Add a new module mapping.
  4. Click OK and confirm when prompted.

Creating Web.config

  1. In your application directory (C:\inetpub\wwwroot\nodeapp), create a web.config file with the following content:

                                                <configuration>
                                                  <system.webServer>
                                                    <handlers>
                                                      <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
                                                    </handlers>
                                                    <rewrite>
                                                      <rules>
                                                        <rule name="DynamicContent">
                                                          <match url="/*" />
                                                          <action type="Rewrite" url="app.js" />
                                                        </rule>
                                                      </rules>
                                                    </rewrite>
                                                    <security>
                                                      <requestFiltering>
                                                        <hiddenSegments>
                                                          <add segment="node_modules" />
                                                          <add segment="iisnode" />
                                                        </hiddenSegments>
                                                      </requestFiltering>
                                                    </security>
                                                  </system.webServer>
                                                </configuration>
                                                

4. Test Your Node.js Application

  1. Open a web browser and navigate to http://localhost:[port] (replace [port] with the port number you configured).
  2. You should see your Node.js API running.

5. Securing Your Application

  1. Enable HTTPS:Use IIS to set up an SSL certificate for your site.
  2. Set Up Authentication:Configure IIS authentication methods if needed.
  3. Implement IP Restrictions:Use IIS to restrict access based on IP addresses.

Conclusion

Hosting a Node.js API on an IISNode server allows you to take advantage of the powerful features of both Node.js and IIS. By following the steps outlined in this guide, you can set up a robust, secure, and scalable environment for your Node.js applications. With the integration capabilities and management tools of IIS, you can ensure that your Node.js API runs efficiently and securely in a Windows environment. Happy hosting!