DEV Community

Cover image for How to deploy React Application on IIS Server

How to deploy React Application on IIS Server

Sumit Kharche on December 08, 2019

Today, I am going to show you how to deploy the React App to the IIS server, so let's grab a cup of coffee and start coding. What is IIS?...
Collapse
 
raminhbbau profile image
RaminHbb

it was an awesome article and solve my issue

Collapse
 
giulianomx profile image
Manuel Rios • Edited

I am glad I've landed on this page. You cannot image the hours I have spent looking for an answer to this. Everybody was saying to add this to my webpack.config.js to fix the problem:

devServer: {
historyApiFallback: true,

But it didn't.

By the way, the web.config you are referring to has to be copied to the directory where the index.html and main-bundle.js which is the root directory of the folder where the files are hosted on IIS. The name of the js file may defer based on your what your configuration is. In my case this is what I have in my webpack.config.js:

output: {

filename: '[name]-bundle.js',

Thank you so much dude. Keep up the great work. Let me know if I can pay you for a fancy cup of coffee.

Collapse
 
sumitkharche profile image
Sumit Kharche

Thanks Manuel. Your words inspired me to share my knowledge more.

Collapse
 
sonalk215 profile image
sonalk215

Hi Sumit,

I followed the steps, but my app is still not accessible.

Error as ---i have attached an image

My web.config file is in public folder
I also followed another link hackernoon.com/adding-web-config-t...

still not working
Please help

Collapse
 
sumitkharche profile image
Sumit Kharche

Can you please provide the screenshot of the error you are facing? Also Could you please make sure that app pool on which your app is running should be run on administrator account so that it can access our files.

Collapse
 
sonalk215 profile image
sonalk215 • Edited

Hi Sumit,

I forgot to add the URLRewrite module on iis, its working fine now, thanks a lot for prompt response
But now I am facing below problem ....

My front-end application(React) and back-end code(DotNet Core), I have kept in the same folder..

If i use 2 differetnt pools --one for Front-end application and another for backend application, then, when the application runs..all the apis fail with 403.18 forbidden error

If i use same pools for both, then the application runs....the apis give status as 200, but the response is in text/html form .....

I am tried various links--like trying DefaultAppPool, but still the issue remains ....

One more thing , if i place my front-end in one folder and call api from another folder, then there is no error ..the application runs fine ..and apis are also working properly

I am very confused....I dont knwo wht is the problem...iis or React
I have uploaded 2 images, with this question, hope you are getting the images too
Please tell me if i should send you any other info, which i am missing

Pleas help

Thread Thread
 
sumitkharche profile image
Sumit Kharche

I am not able to see the images so can you please share the URL for the images. You can DM me as well.

Thread Thread
 
sonalk215 profile image
sonalk215

Hi Sumit,

Thanks for the reply.
I was able to solve the issue. I was giving Capital letters for the api, that's why the problem.

Collapse
 
kavitatandel profile image
kavitatandel • Edited

Thanks Sumit, I was struggling to deploy react app and found your blog. I followed exact steps and my site is working even if I refresh the page. Cheers !!!!

Collapse
 
sumitkharche profile image
Sumit Kharche

Thank you Kavita for feedback. I am really glad this article helped you.

Collapse
 
diegoclemos profile image
diegoclemos

Nice work, thank you, I had some problems running the application, some images were not loading properly, after some investigation, I identified only *.webp image files weren't loading properly... so I found on StackOverflow the following tags that should be inserted on web.config file

    <staticContent> 
        <mimeMap fileExtension=".webp" mimeType="image/webp" /> 
    </staticContent> 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jadhavaa1994 profile image
JADHAVAA1994

I have created a web app with frontend React and nodejs backend. Now I want to host web app on windows IIS web server. Application working fine but react app not connect with backed nodejs.

In vs code development environment it's working fine but on production iis server not working.
How do I resolve this issue?

Collapse
 
calcifer9000 profile image
calcifer9000

I need to host my react app inside a subfolder. localhost:port/subfolder/index.html
The provided web config doesnt work for it.
If I do use it without subfolder it works. It fails without.
Please help.
Thank you

Collapse
 
arunas_dev profile image
Arun A S

I Tried the following steps. but still showing the same error HTTP ERROR 404 attached hrer

Collapse
 
dmbejar profile image
Diego Manuel Béjar • Edited

Just change the configuration for 404 error page. Go to the site configuration, select Error Pages, and change 404 error configuration as show in this image.

dev-to-uploads.s3.amazonaws.com/i/...

Collapse
 
sumitkharche profile image
Sumit Kharche

The main reason for getting this error while deploying react app is when you do not install a URL rewrite module.

Collapse
 
ramesh798 profile image
Ramesh798

I have deployed a react-flow based(react.js library) application in the IIS server. the react flow nodes are placed irrespective of the assigned position in Canvas. but in the localhost server, it is working smoothly.
can anyone help me with this issue?
web.config file is--
<?xml version="1.0" encoding="UTF-8"?>





















Collapse
 
nyanyiwast profile image
Sedrick Tha Dev

Awesome article, just to add on: For this to work, make sure you install URL Rewrite module on IIS by visiting iis.net/downloads/microsoft/url-re... and after installing, stop all websites running and restart IIS. You're good to go!

Collapse
 
davidcastillo4 profile image
DavidCastillo4

Hi Sumit,
First I want to Thank You so much for your Post on “How to deploy React Application on IIS Server”. Took my a week to find a good article and yours worked first try!!!!!!.

Everything works except when I try to refresh on urls with parameter variables.
For example domain.com/user/2
In the above example I want to view details for user 2.
Have you figured out how to get that working for you?
If so would you please share. Thank you so much!!!!

I created a very simple app.js file below to illustrate.
Any reply from you I will promptly reply back.
codesandbox.io/s/browserrouterdemo...

Collapse
 
dannydore profile image
Danny Dore

Interesting alternative to using Apache or Nginx.

Collapse
 
kpabbacus profile image
kpAbbacus

Thanks Sumit Kharche,

Save My Day.

To,
@kpsahani
ReactJs Developer

Collapse
 
thujone profile image
Rich Goldman

Thanks for this, I was pulling my hair out.

Collapse
 
itssameer profile image
itssameer

Hi Sumit,
It worked like a charm for me I host it windows server 2019.
Thanks! very much appreciated

Collapse
 
raminhbbau profile image
RaminHbb

Hi Sumit,
It was perfect and solve my problem!

Collapse
 
kctripathy profile image
Kishor Tripathy

I tried this, but when I refresh it is throwing the error.

Can you please help me to solve this

Collapse
 
sumitkharche profile image
Sumit Kharche

Can you share details of error you are getting?

Collapse
 
sumitkharche profile image
Sumit Kharche

Sure. DM me the error you are getting.

Collapse
 
abubakrkamal profile image
AbubakrKamal

It's working very good thanks for this post

Collapse
 
dnssrinath profile image
DnsSrinath • Edited

This setting worked as expected :) Thank you.

This is the magic pill.

Collapse
 
yusuf987 profile image
Yusuf kaleem khan

All worked well but now i am not able to call api via axios or fetch i had add my api main url in package.json in proxy setting .... can you help me what should i do

Collapse
 
sumitkharche profile image
Sumit Kharche • Edited

The proxy feature isn't meant for production. Proxy is most suitable in the Development environment but when you deploy your app with prod mode on a web server then you need to do it in different ways.
Few links for your references:
create-react-app.dev/docs/proxying...
github.com/facebookincubator/creat...

One way you can do it by using the environment variables.
create-react-app.dev/docs/adding-c...

so for development create file .env.development outside of src folder and add your API URL like:

REACT_APP_API_BASE_URL='<your local api url>'

For Production create file .env outside of src folder & add your Prod api base url like above.

once you configure your API URLs you can simply use it in your as App as below:

process.env.REACT_APP_API_BASE_URL

I hope this solves your problem. Please let me know if any queries.

Collapse
 
jadhavaa1994 profile image
JADHAVAA1994

I have created a web app with frontend React and nodejs backend.
I want to host web app on windows IIS web server.
In IIS Prdouction env.Application working fine but react app failes to connect with backed nodejs.
In vs code development environment it's working fine but on production iis server not working.
How do I resolve this issue?
Showing Error
Request Method: POST
Status Code: 404 Not Found

Collapse
 
joedotnot profile image
joedotnot

Another way to do this is to redirect your 404 to the index.html [you can configure this in IIS directly, I don't remember the exact details]
Then the client-side routing takes over.

Collapse
 
giopoi profile image
giovanni poidomani

only works on locolahost

Collapse
 
bricehemery profile image
bricehemery

Thank you for the article. Can you please explain what's inside the config file?

Collapse
 
jsgurugit profile image
jsguru

Thanks for sharing.
I followed the steps you stated.
I can access the react app on the windows server via localhost:9000
But I can not access via server_ip:9000
could you help me to figure it out?

Collapse
 
atesdanis profile image
Ateş DANIŞ

u have to open port (9000) on firewall inbound rules
and if your server is at your home, u should open port on your modem too

Collapse
 
crazygenexz profile image
Daniel R

With build you refer to the main.js? Im trying to connect a React App to my Windows Server Service but i dont know if i have to mount my React App to connect

Collapse
 
sumitkharche profile image
Sumit Kharche

If you build your React App using npm run build command or whatever command you have for building your app in production mode, it will bundle your app and put it into a build folder. So when we have to deploy this app into IIS then we have to give the path till build folder which contains index.html file.

Collapse
 
advashishta profile image
AD

Just perfect

Collapse
 
venkatuidevloper profile image
Venkatuidevloper

How to publish same static site into default app pool with 80 port. In my project we are using asp.net

Collapse
 
sumitkharche profile image
Sumit Kharche

You have to add a new website and select the default app pool instead of creating a new one. please make sure that default app pool should be run on administrator account so that it can access our files. Let me know if you face any other issues.

Collapse
 
manhhungpm profile image
manhhungpm

Good job bro <3

Collapse
 
kannan profile image
Kannan S

Hi, It's working good thanks for this post, If refresh the page always redirecting to home page its not redirecting to current page where can you help on that.

Collapse
 
iamabdulazeez profile image
I-am-abdulazeez

Thank you bro!

Collapse
 
isay10 profile image
Patricio Giménez

Sir, you are a hero. I was searching a tutorial like this like 5 days ago and now, I can write de goddamn manual for the client haha.
I hope you can understand me, my english isn´t the best.

Collapse
 
mdutta profile image
MDutta

Hi Sumit,
Could you please let me know how to consume webservices ( .svc , wsdl) from React application?
@sumitkharche

Collapse
 
shantdevshukla profile image
shantdevshukla

Thank you it is really very helpful but I am able run in local after npm build but when I trying with same Content of build Code in Server it is not working. Can you help me on this.

Collapse
 
contactboudiss profile image
contactboudiss

thx for that

Collapse
 
advashishta profile image
AD

HI Sumit, it worked great in first attempt.
can we host nodejs+express api project is same on windows iis ??

Collapse
 
tamane9549 profile image
tamane9549

how to handle cors, and how to deploy the server api to iis, please help

Collapse
 
lordbinary11 profile image
lordbinary11

thank you very much Sumit, brief and comprehensive
my search has come to an end😀

Collapse
 
abhishek_pal_ecd09fe49085 profile image
Abhishek Pal

I create a build but i got this error shown in this image

Image description