host node js on windows server (iis) - node.js

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.

Here is a step by step...
if you havent done so install node, iisnode and urlrewrite
add a website to iis
edit the hosts file
add your website url to host
check your new website modules to ensure iisnode is installed
If its there you're good
create the node app code JS file
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");
});
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>
in a browser navigate to the new site and you should get this error because you haven't installed express package
open a command prompt and install express
refresh the web page and voila

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.

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!):
In this new application, create a Rewrite Rule using the Reverse Proxy template, and point to your locally served node js application:
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:
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.
I've got node.js running from the command line, but this could be done as a service if needed.

Related

How to deploy react Remix framework to IIS on windows server 2022?

I developed an App on REMIX framework. But ı do not know how will i publish it on IIS server on Windows Server 2022.
Which opitons should selected for IIS.
npx create-remix#latest
? Where would you like to create your app? (./my-remix-app)
? Where do you want to deploy? Choose Remix if you're unsure, it's easy to change deployment targets. (Use arrow keys)
❯ Remix App Server
? TypeScript or JavaScript? (Use arrow keys)
❯ TypeScript
Remix requires Node.js when running on Windows. You should select Remix App Server.
There are plenty of articles online on how to set up a reverse proxy on IIS. You want to proxy to RAS running on localhost:3000 (by default).
https://www.google.com/search?q=iis+nodejs+reverse+proxy
The best way for the React Remix framework is to select Remix App Server, then run remix build to build the app for production, and run npm start to run the server. After performing the above operations, please treat it as a normal Node.js server, and follow the Conventional Way - deploying a node.js application on windows IIS using a reverse proxy.
Install Node.js on Windows Server
Deploy and test Node.js applications
Create a website for our Node.js application on IIS
Configure Reverse Proxy on IIS
Thanks for answers which showed me a way for found a solition.
Install Node.js on Windows Server .
Install IIS Node on Windows Server.
Install URL Rewrite on Windows Server.
Upload all files to under your web site folder on wwwroot except .cache,build,public/build,node_modules folders.(we will install them on server.)
Under web site folder, type command on cmd npm install
Type command npm run dev
Create a web.config file main path of web site. Codes are :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<iisnode loggingEnabled="false" nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" />
<handlers>
<add name="iisnode" path="/build/index.js" verb="*" modules="iisnode" />
</handlers>
<security>
<requestFiltering>
<hiddenSegments>
<add segment="node_modules" />
<add segment="iisnode" />
</hiddenSegments>
</requestFiltering>
</security>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:3000/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Azure web app giving HTTP error 500 when viewed, how can this be fixed?

trying to upload my mobile web application to azure
followed
https://learn.microsoft.com/en-us/azure/app-service/quickstart-nodejs?tabs=windows&pivots=development-environment-vscode
ended up just getting the error unable to handle request HTTP error 500
the web app works locally so i dont know what the issue is
a friend told me it was might be because all webpages in the views folder are .ejs files when they should be HTML.
I have followed the same document which you have provided and able to access the Application without any issues.
Create Node.js Application using the below command.
npx express-generator myExpApp19Dec --view ejs
Navigate to the Application root directory(myExpApp19Dec) and run npm install.
node_modules folder will be created at root of the directory.
Run npm start to start and run the application in local.
Open the Application from VSCode.
Steps to deploy Web App to Azure App Service
Sign into Azure => Click on Azure Icon => Select your Subscription = > App Services.
web app name - ExpressApp19Dec
runtime stack - Node 18 LTS
OS - Windows
Location - East US
Immediately after deployment, when I tried to access the Application, I got the below error.
Added SCM_DO_BUILD_DURING_DEPLOYMENT in Application Setting as suggested in the document.
Navigate to the deployed App folder in VSCode => Your App => Application Settings =>Add New Setting.
We can even add this Application Setting from Azure Portal => App Service => Configuration section. Re-deploy option can be excluded when we add Application Setting from Azure Portal.
Re-deploy the Application to get the latest changes.
Make sure web.config file is created at the root directory of the deployed Application in KUDU Console.
Path to KUDU Console -
https://YourAppServiceName.scm.azurewebsites.net/DebugConsole
My autogeneratedweb.config file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<webSocket enabled="false" />
<handlers>
<add name="iisnode" path="bin/www" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^bin/www\/debug[\/]?" />
</rule>
<rule name="StaticContent">
<action type="Rewrite" url="public{PATH_INFO}"/>
</rule>
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="bin/www"/>
</rule>
</rules>
</rewrite>
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin"/>
</hiddenSegments>
</requestFiltering>
</security>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</configuration>
Now Iam able to access the Application.
all webpages in the views folder are .ejs files when they should be HTML.
In VSCode => root folder => app.js file,
app.set('view engine', 'ejs');
This code helps to detect the ejs files. The issue is not with the ejs files.
EJS is an Embedded JavaScript template which is used by Node JS Application.

How to build Node js app in production for hosting in IIS

I have a node js server that I want to deploy on IIS. Since I am new to node, I am unable to understand the following:
How to bring the code in production mode?
How to build the code in production mode?
How to deploy it to IIS
Thank you
You can follow the below steps to deploy the node js app in iis:
1)Install iisnode
https://github.com/Azure/iisnode
2)Download and install url rewrite extension
3)set the port number in your node app by using below code:
process.env.PORT
note: use the same port number in iis site bindings
4)add the site in iis and give your node js app folder path and set the site bindings
5)Add below code in web.config file:
<configuration>
<system. Webserver>
<handlers>
<add name=""iisnode"" path=""server.js"" verb=""*"" modules=""iisnode"" />
</handlers>
<rewrite>
<rules>
<rule name=""rule1"">
<match url=""/*"" />
<action type=""Rewrite"" url=""server.js"" />
</rule>
</rules>
</rewrite>
</system. Webserver>
</configuration>
Note: please give the iis_iusrs and iusr full control permission to the site root folder

How to deploy node.js application and node.js express applicaion on IIS

I have created Node.js application on Visual studio 2019, and I want to deploy it on IIS.
How I can deploy node.js application and Node.js express applications on IIS on local system
I have tried many solutions but none of them completely helpful
The link below some how helpful but confusing and lot of content to read
https://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx
Here some simple solution for above problem
Node.js deplyment on iss
Install IISNODE
open cmd as admin
cd "C:\Program Files\iisnode" (or "C:\Program Files (x86)\iisnode" if you installed the 32bit version
Type setupsamples.bat
Install MS URL rewrite using web installer
add web.config with code
<configuration>
<system.webServer>
<!-- indicates that the server.js file is a node.js application
to be handled by the iisnode module -->
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="sendToNode">
<match url="/*" />
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
publish node.js project
paste published site on inetpub-->wwwroot
run cmd as admin
run command on website folder
For node
npm install
For Node Express
npm install express
Create IIS website
start browsing

iisnode performance is very slow

I found out that iisnode is slow compare to use node command.
There are many benefits of using iisnode, but the performance is bad.
I am using the configuration file from here.
Any idea how to speed it up?
UPDATE:
I notice each page call will reconnect a new mongodb connection.
How can I prevent it?
Here is a hint to integrate Node.js with IIS7 on a Windows 64-bit Server. The following hint also solves some poor performance issues with iisnode and demonstrates how to use Node.js Native Extensions as well.
Summary:
Create a new Unmanaged Integrated 32-bit Application Pool
dedicated for node.js. No other applications should use this pool.
This works for 64-bit as well but some node.js Native Extensions such as Coconut2D require 32-bit because of SQLite wrapping. If you are not using Native Extensions then you can go 64-bit all the way!
Download: iisnode-core-iis7-v0.1.19.0-x64.msi.
Install it using this command: msiexec /i iisnode-core-iis7-v0.1.19.0-x64.msi WOW=1. This will install the 32-bit version of iisnode on a 64-bit machine. Note that iisnode.dll will be installed in C:\Program Files (x86)\iisnode\iisnode.dll.
Download the 32-bit version of node.js (eg. node-v0.12.0-x86.msi) and install it in C:\nodejs
Create a new Application Pool
Name: node.js
Managed pipeline mode: Integrated
.NET Framework Version: No Managed Code
Enable 32-Bit Applications: True
Identity: administrator
Assuming your Node.js Server Script file is server.js. Go the the web folder and create file node_start.cmd. In the command file you should change the current path to your wwwroot and start node.js with your server.js file. You should use double quoted paths.
C:
cd "C:\HostingSpaces\...\wwwroot"
"C:\nodejs\node.exe" "C:\HostingSpaces\...\wwwroot\server.js"
In your server.js make sure you have process.env.PORT
var http = require('http');
http.createServer(function (req, res) {
... your code here ...
}).listen(process.env.PORT);
(Optional) If you are using any node.js Native Extensions such as Coconut2D, SQLite, Cairo or WebKit modules, you must copy the *.node files and DLLs in your wwwroot\node_modules folder. Make sure you also set NTFS security to allow execution of those files or elevate the Application Pool to impersonate the Administrator. To load the native extensions use require() as shown below.
var http = require('http');
var Coconut2D = require("Coconut2D.node");
http.createServer(function (req, res) {
... your code here ...
}).listen(process.env.PORT);
Place the following web.config in your web root (eg. C:\HostingSpaces\...\wwwroot). This will enable IIS7 to process any non-node files such as images, static html files and xml files, and let node.js handle only its own server-side scripts.
Having IIS handling static files and running server-side scripts on
node.js side-by-side is a highly recommended practice and really boosts the
performance of your web sites.
In this example I am handling .asp files with iisnode by using a Rewrite Rule. Replace *.asp* with your node.js server script extension (eg. *.njs*). Note that there is not a root slash in the wildcard pattern; this is important as well as the last * at the end of the pattern.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="node.js" verb="*" modules="iisnode" />
</handlers>
<iisnode nodeProcessCommandLine=""C:\...\start_node.cmd"" />
<defaultDocument>
<files>
<remove value="index.php" />
<remove value="default.aspx" />
<remove value="iisstart.htm" />
<remove value="index.html" />
<remove value="index.htm" />
<remove value="Default.htm" />
</files>
</defaultDocument>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
<rewrite>
<rules>
<rule name="CavoBoutique" patternSyntax="Wildcard">
<match url="*.asp*" />
<action type="Rewrite" url="node.js" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Resources