Over the weekend I was bored and tried hosting node applications on IIS on a server. Since currently I have only tried wordpress I had no idea how to go about this which might be why you are here, so lets dive straight into how to get it running. So first of all if you haven’t done this before you will probably not have node
installed on your server. So first lets install everything that we will need.
Prerequisites
- If you do not have IIS (Internet Information Services) enabled; you will have to enable it.
SearchTurn Windows Features On or Off
and check the box next toInternet Information Services
. Expand theInternet Information Services
node to select additional features likeWeb Management Tools
andWorld Wide Web Services
. Ensure at least the following are selected:Web Management Tools
: IncludesIIS Management Console
.World Wide Web Services
: Includes various services for hosting websites and applications.
- Install
node
from here. Make sure that you add the path tonode.exe
in yourenvironment variables
. In most cases it would beC:\Program Files\nodejs\
. - Install
iisnode
from here. - Install
URL Rewrite
from here.
Creating and Setting up the Site
- Open
IIS Manager
and click on your device in theconnections
panel on the left to expand it. - Right click
Sites
and click on theAdd Website...
option to create a new site, and add the domain name you want it to show up on in thehostname
and check thestart website immediately
. - Add
127.0.0.1
to your Site from thebindings
in the right pane in your site’s page in IIS. - Check if
iisnode
is installed inmodules
. - Locate the folder
C:\intepub\wwwroot\
and create a folder with the same name as your site. - Add the following files to
C:\inetpup\wwwroot\{site name}
:server.js
web.config
- Open the terminal and install
ExpressJs
usingnpm install express
. - Write your server script in
server.js
to create anexpress
server.var express = require("express"); var app = express(); app.get("/ping", function(req, res) { res.send("Pong!"); }); app.listen(process.env.PORT, () => { console.log("listening"); });
- Add the following to
web.config
<configuration> <system.webServer> <handlers> <add name="iisnode" path="server.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> <httpErrors existingResponse="PassThrough" /> <iisnode nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" /> </system.webServer> </configuration>
This
web.config
file is written inXML
and is used to configure settings for web applications hosted on Internet Information Services (IIS). It allows you to define how IIS should handle requests, manage security settings, rewrite URLs, handle custom error messages, and integrate with specific modules likeiisnode
for hosting Node.js applications.
Note: If you are having trouble creating or editing files in C:\inetput\wwwroot\{your site}
, right click and open properties. Add your user if it does not exist and check the full control
box to make the process of editing and creating files easier. In most cases this requires administrative control.
And with that we have served node applications on IIS!
Be First to Comment