Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 503
jamian Avatar asked Sep 17 '17 17:09

jamian


People also ask

Can we host node js in IIS?

IISNode is an open source native IIS module written in C++ that allows node. js (node.exe) to be run inside Windows IIS.

How do I deploy node js Windows server with IIS?

Go to https://www.iis.net/downloads/microsoft/url-rewrite. Click “Install this extension”. It will open the “Web Platform Installer” start initiating. Press “Install” and “I Accept” (I hope you read the documentation 😉).

Can we install Node JS in Windows server?

Node. js can also be installed via a package manager. The package manager for windows is known as Chocolatey. By running some simple commands in the command prompt, the Chocolatey package manager automatically downloads the necessary files and then installs them on the client machine.


1 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

like image 141
Tom McDonald Avatar answered Oct 22 '22 11:10

Tom McDonald