Enable IIS and Host nodejs application on IIS
Published: 2023-05-19 12:38:10

Hello guys, So recently while working on a project I had to work with IIS(Internet Information Services) which I had never worked with in my entire life.So figuring out how to host node application on it was quit time consuming and after lots of surfing through internet I finally managed to host my node application on IIS.
Just writing this blog so you don't have to surf through articles and you will get all information in this one blog.
So lets Start....

## 1. How to Enable IIS
Go to 

> START > CONTROL PANEL > PROGRAMS > START WINDOWS FEATURES ON OR OFF

![Image of Control Panel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urfq2ckr2axjc49ptjeh.png)

After clicking "OK", "CHECK" each and every dropdown under (NET Framwork 3.5) and (Internet Information Services)

![Windows Features on/off settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8z0yhfu14b53fxoxofp.png)
Then Click "OK" and wait for changes to Apply .

---

## 2. Opening IIS (Internet Information Services)

Ones All changes are applied go to 
> START > INTERNET INFORMATION SERVICES (IIS) MANAGER

And open it. 
You will see this screen 

![IIS HomeScreen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56ogljxcts70o71v7pfl.png)

Now that we have IIS lets move towards our nodejs part 

---

## 3. Setting up Nodejs

- I hope you already have Nodejs installed if not download the Node.js from below link and install normally

Link : [Node.js](https://nodejs.org/en)

- Download IISNode from the below link and install normally

Link : [IISNode](https://github.com/azure/iisnode/releases/download/v0.2.21/iisnode-full-v0.2.21-x64.msi)

- Install URLRewrite from the below link and install normally

Link: [URL Rewrite:The Official Microsoft IIS Site](https://www.iis.net/downloads/microsoft/url-rewrite)

The above softwares will automatically configure with IIS and nodejs and we do not have to make any changes to them.

- In my project I have simple crud operations in index.js 
Now in same folder create a file called "Web.config" and add the code given below

> **_NOTE:_** Make changes accordingly in code and put your filename in place of  “<your-file-name>” for me it is index.js so I will simply put index.js


**web.config:**

```python
<configuration>
<system.webServer>
<handlers>
  <add name="iisnode" path="<your-file-name>" 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="/<your-file-name>" />
    </rule>
  </rules>
</rewrite> 

<security>
  <requestFiltering>
    <hiddenSegments>
      <add segment="node_modules" />
      <add segment="iisnode" />
    </hiddenSegments>
  </requestFiltering>
</security>
</system.webServer>
 </configuration>
```


---

## 4. Making Nodejs and IIS Connection

Now go to IIS home screen in left panel Right Click on sites > Click Add Website.

Then feel details accordingly for your application give the sitename Eg: "samplenode" then next select your application folder and then enter the port number for me I am using port 5000 yours can be different. Then click "ok"

![IIS New website configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vd9rhp472o4wnhpk2kqt.png)

Now just Navigate to "localhost:<port-number>" in your browser Eg: localhost:5000


![Image of Localhost in browser](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fx97w8cv0fz6h7wwgn68.png)

Ones everything is done and your node application is running you will now also see a "iisnode" folder in your application directory.

If you are getting any error related to node environment variable
Just add this code between 


```
</security>
<iisnode
      nodeProcessCommandLine="&quot;%programfiles%\nodejs\node.exe&quot;" 
      interceptor="&quot;%programfiles%\iisnode\interceptor.js&quot;" />
</system.webServer>
```
---

## 5. Making changes to your Server settings

Now to make changes to your server setting or to restart/stop/start server you can go to any of the settings in the right panel after selecting your site. As shown below 

 
![IIS website settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3u1ubku47s01jdtszezc.png)


Thankyou for reading, Hope the blog helped, Do like and Subscribe to the blog and if have nay query please comment. ❤️