15

I started learning server side coding a month ago, I build a nodejs project and webservices with get and post requests using 'express' framework and mssql. My project file includes a 'main.js' file and a 'node_modules' folder.

I'm trying to host this project on IIS but have no idea or experience on how to do so.

Will i have to package my project in some way.

Can i host nodejs projects on IIS? If so, then what are the steps that I need to do so. I have a windows server running IIS with mysql installed there.

jamian
  • 1,544
  • 2
  • 16
  • 25

3 Answers3

42

Here is a step by step...

  1. if you havent done so install node, iisnode and urlrewrite
  2. add a website to iis enter image description here
  3. edit the hosts file enter image description here
  4. add your website url to host enter image description here
  5. check your new website modules to ensure iisnode is installed enter image description here
  6. If its there you're good enter image description here
  7. create the node app code JS file enter image description here
  8. Put this code in the file
var express = require("express");
var app = express();
app.get("/", function(req, res) {
  res.send("Hello Worlxxxxd!");
});
// This is REQUIRED for IISNODE to work
app.listen(process.env.PORT, () => {
  console.log("listening");
});
  1. add a web.config file to the directory and put this code in it
<configuration> 
    <system.webServer>
  
     <handlers>
       <add name="iisnode" path="node_app.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>
     </system.webServer> 
 </configuration>
  1. in a browser navigate to the new site and you should get this error because you haven't installed express package enter image description here

  2. open a command prompt and install express enter image description here

  3. refresh the web page and voila enter image description here

Kugan Kumar
  • 423
  • 1
  • 8
  • 14
Tom McDonald
  • 1,532
  • 2
  • 18
  • 37
  • Nice. However, I was stuck at 10. Couldn't get to debug. – Monster Brain Feb 05 '20 at 12:50
  • Thank you for the perfect guide with screenshots. Not only learned how to deploy node in IIS but also how to setup smoothly custom host name. – sapatelbaps Jun 21 '20 at 05:10
  • A Tip: Download Node from the Node website rather than using this hotlink, I ended up installing a x86 version on a 64 bit server, which doesn't work! – Fiach Reid Nov 12 '20 at 09:17
  • The `` section is giving me this error "This configuration section cannot be used at this path.". What is the safest workaround? – Salman A Jul 01 '22 at 12:00
  • For anyone coming after me, I got stuck at step 11. ChatGPT helped me over it with this line "Navigate to the root directory of your application in the command prompt and run the installation command again". Turns out the "npm install express --save" line has to be executed from a command prompt opened to the root of the new website folder because it will install the nodejs hooks for that site into that folder. – Jereme Jun 12 '23 at 21:07
4

I'm a little late to the party, so you've probably either solved this problem or gone a different route.

You can run node applications inside of IIS using iisnode.

I, personally, have had mixed success getting iisnode running, but it is definitely possible.

Andy Arndt
  • 385
  • 1
  • 7
2

I'd recommend using the URL Rewriting (https://www.iis.net/downloads/microsoft/url-rewrite) and Application Request Routing (https://www.iis.net/downloads/microsoft/application-request-routing) IIS modules. Install these on your server hosting IIS.

In IIS create an application that points to the directory where your node application is running (although this path is not actually used!): Add IIS Application

In this new application, create a Rewrite Rule using the Reverse Proxy template, and point to your locally served node js application: Add Rewrite Rule

And now, you can browse to your IIS hosted site, using the IIS application you had configured, and it will show your node.js hosted site:

View node.js app in IIS

One of the main benefits of this approach is that the SSL cert issued to IIS can be used with an "http" hosted node.js application.

Browsing to https URL

I've got node.js running from the command line, but this could be done as a service if needed.

Shiraz
  • 2,310
  • 24
  • 26
  • To run "as a service", you can use Task Scheduler. I'd recommend a batch file that sets environment variables and then calls node – Shiraz Mar 09 '23 at 11:45