host node js on windows server (iis)
Asked Answered
P

3

21

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.

Prunella answered 17/9, 2017 at 17:1 Comment(1)
Check : weblogs.asp.net/owscott/…Leoraleos
H
52

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

Harveyharvie answered 12/1, 2019 at 20:13 Comment(8)
Nice. However, I was stuck at 10. Couldn't get to debug.Pancreatin
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.Slipknot
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!Inexpressive
The <handlers> section is giving me this error "This configuration section cannot be used at this path.". What is the safest workaround?Tuesday
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.Garb
Is this solution also works for Windows server 2022?Entomb
Step 10 doesn't make any sense to me. Why should you want to see this error? Why not simply install the express package first?Tantalic
@TheincredibleJan it's fairly common when setting things up / testing to start with the errors. Kind of like red/green testing in software development. The thing here is that you're expecting a specific error so if something else comes up you know you have a problemLewislewisite
T
5

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.

Tilefish answered 28/11, 2017 at 5:16 Comment(0)
S
5

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.

Sansom answered 18/1, 2023 at 15:14 Comment(3)
To run "as a service", you can use Task Scheduler. I'd recommend a batch file that sets environment variables and then calls nodeSansom
Why do you add "test" to your URL? You didn't define any rules. Everything will be redirected to port 3000.Tantalic
The rewrite rule is in the "/test" application if you followed my steps, hence the need for it in the URL. Everything under test will be redirected to localhost:3000 - /test?name=value, /test/with/longer/url, etc If you created another alias in step 1 (e.g. Jan), then the URL would be <web server>/JanSansom

© 2022 - 2025 — McMap. All rights reserved.