host node js on windows server (iis)

Viewed 26863

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.

2 Answers

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

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.

Related