06-19-2024, 04:41 PM
So, you’re looking to host your Node.js application on IIS? That’s awesome! I remember when I first did it; it felt like cracking a code or something. Let's get into how you can set this up because it's not as complicated as it might sound.
First, you need to install IIS if you haven’t done that yet. On Windows, it's usually just a feature that you can enable. If you're on Windows 10 or 11, you can go to the Control Panel, then to Programs, and click on “Turn Windows features on or off.” You’ll find Internet Information Services in the list. Just make sure you tick that box and install it. While that’s going on, you might also want to install the URL Rewrite module. This is essential because it allows you to route HTTP requests and helps your Node.js app sit nicely under IIS.
Now, after you’ve got IIS up and running, you’ll need Node.js itself. Of course, you probably have it installed already because you’re hosting a Node app, right? But in case you don’t, you can easily grab the installer from the official Node.js website. Just download that, run the installer, and you should be good to go.
Once you’ve got Node.js in place, the real fun begins with setting up your application. What I usually do is create a dedicated directory in your system to hold your app. You could create a folder called “MyNodeApp” or something similar. This helps in keeping things organized. Now, put your Node.js application code in there.
When you’ve got your application in place, you usually start it up using a command like node app.js. But here’s the catch with running Node.js apps on IIS. You don’t just want to run it in a command prompt. Instead, we use a tool called iisnode. This is the bridge between your Node.js application and IIS. iisnode allows IIS to host Node.js applications and serves as a handler for incoming requests.
To get iisnode, you’ll want to download and install it. Their GitHub page has good instructions on how to do it. Once you've got it standing by, it's time to configure IIS to know about your Node.js app and how to handle incoming requests.
When you’re in IIS Manager, you’ll want to create a new site. Right-click on "Sites" and select “Add Website.” This is where you set the alias, which acts as your website's name. Let's say you call it "MyNodeApp." You also need to pick a physical path. This should be the folder where your app lives. Choose your MyNodeApp folder here. Next, choose a port. You might want to go with 3000 or any other port not already in use.
Now comes an essential step—set the application pool for your new site. IIS uses application pools to run web applications independently. When you’re creating that site, you’ll see an option for the application pool. You can create a new pool or use an existing one. If you're creating a new one, you should set the .NET CLR version to “No Managed Code” because you're running a Node application, not a .NET one. Selecting this option prevents any issues that might arise from IIS trying to work with .NET code.
After you’ve set that up, you need to connect the iisnode module to your Node.js app. This is done via a web.config file, which is essentially how IIS understands how to handle your requests. You’ll need to create a web.config file in your app’s root directory. Here’s how you can set it up:
Open your favorite text editor, and you’ll want to write something like this in your web.config:
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="app.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="Nodejs" stopProcessing="true">
<match url="^app.js$" />
<action type="Rewrite" url="app.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Just replace app.js with whatever your main Node file is named. This configuration tells IIS to use iisnode to handle requests meant for your app.js file.
At this point, you've got your IIS site set up with a link to your Node application. You’ll need to make sure that your application is running. And while you can test this through the command line like I mentioned earlier, you want to also run it in a way that IIS can handle it properly.
For this purpose, I like to use a process manager like PM2. It’s a handy tool for managing Node.js applications in production. If you haven’t used it before, all you need to do is run npm install pm2 -g to install it globally onto your system. After that, you can start your application using pm2 start app.js. This way, PM2 keeps your app running and will automatically restart it if it crashes.
Now, before you open your browser and hop onto localhost, make sure that the firewall is not blocking your port. Open your Windows Defender Firewall settings, and check your inbound rules. You might need to create a new rule that allows traffic through the port you set. It only takes a couple of minutes, but it's key for ensuring that you can access your app from the web.
With everything set up, you can now visit http://localhost:3000 (or whatever port you chose) in your browser. If everything is working smoothly, you should see your Node.js application running!
It can be really satisfying to see that your hard work has paid off. But what if you want to access the application from another machine? That’s just a matter of finding your machine's local IP address. You can find this by running ipconfig in the command prompt. Use the IPv4 address, and try accessing your app from another device on the same network via http://YOUR_IP_ADDRESS:3000.
Every time I set this up for someone, there’s always a bit of tweaking involved. Sometimes, permissions might need adjustments, especially if you’re using Windows Server. You may run into issues like the website not loading. In those moments, checking the logs can be super helpful. They’re often found in your app directory if you set that up in your iisnode configuration.
I hope you found my post useful. By the way, do you have a good Windows Server backup solution in place? In this post I explain how to back up Windows Server properly.
First, you need to install IIS if you haven’t done that yet. On Windows, it's usually just a feature that you can enable. If you're on Windows 10 or 11, you can go to the Control Panel, then to Programs, and click on “Turn Windows features on or off.” You’ll find Internet Information Services in the list. Just make sure you tick that box and install it. While that’s going on, you might also want to install the URL Rewrite module. This is essential because it allows you to route HTTP requests and helps your Node.js app sit nicely under IIS.
Now, after you’ve got IIS up and running, you’ll need Node.js itself. Of course, you probably have it installed already because you’re hosting a Node app, right? But in case you don’t, you can easily grab the installer from the official Node.js website. Just download that, run the installer, and you should be good to go.
Once you’ve got Node.js in place, the real fun begins with setting up your application. What I usually do is create a dedicated directory in your system to hold your app. You could create a folder called “MyNodeApp” or something similar. This helps in keeping things organized. Now, put your Node.js application code in there.
When you’ve got your application in place, you usually start it up using a command like node app.js. But here’s the catch with running Node.js apps on IIS. You don’t just want to run it in a command prompt. Instead, we use a tool called iisnode. This is the bridge between your Node.js application and IIS. iisnode allows IIS to host Node.js applications and serves as a handler for incoming requests.
To get iisnode, you’ll want to download and install it. Their GitHub page has good instructions on how to do it. Once you've got it standing by, it's time to configure IIS to know about your Node.js app and how to handle incoming requests.
When you’re in IIS Manager, you’ll want to create a new site. Right-click on "Sites" and select “Add Website.” This is where you set the alias, which acts as your website's name. Let's say you call it "MyNodeApp." You also need to pick a physical path. This should be the folder where your app lives. Choose your MyNodeApp folder here. Next, choose a port. You might want to go with 3000 or any other port not already in use.
Now comes an essential step—set the application pool for your new site. IIS uses application pools to run web applications independently. When you’re creating that site, you’ll see an option for the application pool. You can create a new pool or use an existing one. If you're creating a new one, you should set the .NET CLR version to “No Managed Code” because you're running a Node application, not a .NET one. Selecting this option prevents any issues that might arise from IIS trying to work with .NET code.
After you’ve set that up, you need to connect the iisnode module to your Node.js app. This is done via a web.config file, which is essentially how IIS understands how to handle your requests. You’ll need to create a web.config file in your app’s root directory. Here’s how you can set it up:
Open your favorite text editor, and you’ll want to write something like this in your web.config:
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="app.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="Nodejs" stopProcessing="true">
<match url="^app.js$" />
<action type="Rewrite" url="app.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Just replace app.js with whatever your main Node file is named. This configuration tells IIS to use iisnode to handle requests meant for your app.js file.
At this point, you've got your IIS site set up with a link to your Node application. You’ll need to make sure that your application is running. And while you can test this through the command line like I mentioned earlier, you want to also run it in a way that IIS can handle it properly.
For this purpose, I like to use a process manager like PM2. It’s a handy tool for managing Node.js applications in production. If you haven’t used it before, all you need to do is run npm install pm2 -g to install it globally onto your system. After that, you can start your application using pm2 start app.js. This way, PM2 keeps your app running and will automatically restart it if it crashes.
Now, before you open your browser and hop onto localhost, make sure that the firewall is not blocking your port. Open your Windows Defender Firewall settings, and check your inbound rules. You might need to create a new rule that allows traffic through the port you set. It only takes a couple of minutes, but it's key for ensuring that you can access your app from the web.
With everything set up, you can now visit http://localhost:3000 (or whatever port you chose) in your browser. If everything is working smoothly, you should see your Node.js application running!
It can be really satisfying to see that your hard work has paid off. But what if you want to access the application from another machine? That’s just a matter of finding your machine's local IP address. You can find this by running ipconfig in the command prompt. Use the IPv4 address, and try accessing your app from another device on the same network via http://YOUR_IP_ADDRESS:3000.
Every time I set this up for someone, there’s always a bit of tweaking involved. Sometimes, permissions might need adjustments, especially if you’re using Windows Server. You may run into issues like the website not loading. In those moments, checking the logs can be super helpful. They’re often found in your app directory if you set that up in your iisnode configuration.
I hope you found my post useful. By the way, do you have a good Windows Server backup solution in place? In this post I explain how to back up Windows Server properly.