jamian
jamian

Reputation: 1652

host node js on windows server (iis)

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.

Upvotes: 23

Views: 69120

Answers (3)

Shiraz
Shiraz

Reputation: 2820

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.

Upvotes: 8

Tom McDonald
Tom McDonald

Reputation: 1892

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

Upvotes: 53

Andy Arndt
Andy Arndt

Reputation: 395

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.

Upvotes: 5

Related Questions