Categories
Development

Serve React App on IIS

Let’s say you have a front-end project developed using React or another JavaScript library/framework. Now, let’s assume that your server OS is a Windows Server and by default, you will use IIS as your web server. In this case, it’s not a routine job to deploy your project like an ASP.NET website. You have to do a little more to get your website to work. Read this concise article to learn how to do that.

Requirements

First of all, you need to install these things on your IIS server machine:

  1. URL rewrite
  2. iisnode
    It will add node to your IIS modules.
  3. And of course, you need to install node itself.

Configuring the IIS

I assume that you’ve created your site in IIS, and your built code is in the site folder. If it’s a React project, it should have some files and folders like public, index.html, etc.

So, check the modules of your site in IIS Manager and be sure that it has the iisnode on the list. If you don’t see that, you have to install iisnode again or add it manually.

iisnode in modules of a site in IIS Manager

Also, you have to create a web.config file, and put these following lines in this config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
	  <handlers>
		<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
	  </handlers>
	  
	  <rewrite>
		<rules>
		<rule name="nodejs" stopProcessing="true">
		  <match url="(.*)" />
		  <conditions logicalGrouping="MatchAll">
			<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
			<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
			<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
		  </conditions>
		  <action type="Rewrite" url="/" />
		</rule>
		</rules>
	  </rewrite>
	  
	  <security>
		<requestFiltering>
		<hiddenSegments>
		  <add segment="node_modules" />
		  <add segment="iisnode" />
		</hiddenSegments>
		</requestFiltering>
	  </security>
  </system.webServer>
</configuration>

It’s done.

Now, it’s possible for you to check your website in a browser to see if it really works :).

See Also

I used these following pages to do it for my project:

And some other pages that I don’t remember their addresses.

Happy Reacting!

Leave a Reply

Your email address will not be published. Required fields are marked *