React app deployed to IIS StaticFile handler problem - reactjs

I have a sample React application. I deployed the application to IIS. Default page is OK, but /category page returns 404 - File or directory not found.
import { Route, BrowserRouter } from "react-router-dom";
const Home = () => (
<div>
<h2>Home</h2>
</div>
);
const Category = () => (
<div>
<h2>Category</h2>
</div>
);
export default function App() {
return (
<BrowserRouter>
<Route path="/"><Home /></Route>
<Route path="/category"><Category /></Route>
</BrowserRouter>
);
}
Web.config file content. runAllManagedModulesForAllRequests solution not working unfortunately.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="UrlRoutingModule"/>
</modules>
</system.webServer>
</configuration>
Detailed Error Information
Module: IIS Web Core
Notification: MapRequestHandler
Handler: StaticFile
Error Code 0x80070002
ny suggestion please

Using URL rewrite modul, redirect all requests to index.html file.
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="React Routes" 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>
</system.webServer>
</configuration>

Related

Why does react routers can't handle predefind route

In App.js component I was defined route like this:
<Route path="/item/:itemId" exact element={<Item />} />
But when I manually enter the URL in the browser like :
http://example.com/item/3
The browser shows me an error page:
404 - File or directory not found.
How can I fix the error?
Finally I got it.
According to this article, I should install the URL Rewrite module first. then
create a web.config beside project build files. and put these inside it :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReactRouter Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
So now my problems solved
If you are using router v6 there is not exact keyword

.net core 3.1 web.config with routing

I tried publish my project on hosting, but I have some issues.
I have SPA (react) in same folder as .netCore(rest api).
I want SPA routing, thats why I set rule in web.config, but it is not working. When I remove handlers ("aspNetCore") section from config, it is works, but restApi doesnt.
Can anyone help me with web.config?
I just want routing with react and have working restapi on same domain myDomain.com/api.....
Thank you.
My web.config.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<rewrite>
<rules>
<rule name="redirect all requests" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="index.html" appendQueryString="true" />
</rule>
</rules>
</rewrite>
<aspNetCore processPath=".\Horseedo.Api.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
</system.webServer>
</location>
</configuration>
<!--ProjectGuid: 654874ba-ab44-4982-a101-ee05ba73cc4e-->

How to serve a static html file with react app in iframe when using react router?

I'm currently having problems when serving a HTML file in my react app. I use #nrwl/react to build my application.
The html folder is present after building my application (in the public folder).
My setup works when running the application locally. The moment I deploy the application and host it I think the react router is messing up the path. In the deployed version I see my application nested with a blank screen in my iframe.
Router:
<Switch>
<Route path="/" exact />
<Route path="/home" component={HomeComp} />
<Route path="/iframe" component={IframeComp} />
<Route onEnter={() => window.location.reload()} />
<Route component={NotFoundPage} />
</Switch>
IframeComp:
import React from 'react';
const IframeComp= () => {
return (
<iframe
src="/staticfolder/index.html"
></iframe>
);
};
export default IframeComp;
My rewrite rules in web.config
<rewrite>
<rules>
<rule name="HTTPSs">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
<add input="{HTTP_HOST}" negate="true" pattern="^localhost" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
<rule name="ReactRoutes" stopProcessing="true">
<match url="^(?!api|swagger).*" />
<conditions>
<add input="{HTTP_METHOD}" pattern="^GET$" />
<add input="{HTTP_ACCEPT}" pattern="^text/html" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>

Azure App Service Deploy does not deploy web app successfully

I am a newbie to React
I created a node app service in the Azure Portal using the Node JS Empty web App Starter web app.
When I go to the URL I see
Hello, world!
Which I note is in the contents of server.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('Hello, world!');
}).listen(process.env.PORT || 8080);
I see that web.config refers to server.js
<configuration>
<system.webServer>
<handlers>
<!-- indicates that the app.js file is a node.js application to be handled by the iisnode module -->
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<!-- Don't interfere with requests for logs -->
<rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$" />
</rule>
<!-- Don't interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^server.js\/debug[\/]?" />
</rule>
<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}" />
</rule>
<!-- All other URLs are mapped to the Node.js application entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
</conditions>
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
When I deploy my static react website files to the site, I can see the files are there but the web site itself does not change when I go to the URL.
I think what I need is for the website to start src\app.js instead of server.js
How do I do that?
I tried replacing server.js with ./src/App.js but then the App.js just displays as text.
This Burke Holland shows how to set up the pipeline ( near the bottom)
And also links to this article by Tony Patrina that shows how to set up web.config.
As follows;
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="React Routes" 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>
</system.webServer>
</configuration>

Making React Router working with IIS

I've been looking for a way to make my app working with IIS. I can access my homepage just fine but when I navigate to my route http://10.7.138.131/home/wellchart I'll received 403 error.
So I have a web.config that looks like this :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReactRouter Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
My createBrowserHistory :
export const History = createBrowserHistory({
basename:"/home"
});
And lastly my react routing :
<Router basename={"/"} history={History}>
<div>
<Route path="${process.env.PUBLIC_URL}/wellchart" component={WellChartPage} />
<Route path="${process.env.PUBLIC_URL}/welldetail" component={WellDetailPage} />
<Route path="${process.env.PUBLIC_URL}/tableau" component={TableauPage} />
</div>
</Router>
Thanks :)
You need setup url rewrite on IIS.
In react application we have single entry point index.html. If you try to open other page, IIS will try to find that file but we don't have any physical file there so IIS will return 404.
URL rewrite will solve this problem
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
https://www.iis.net/downloads/microsoft/url-rewrite

Resources