I have a web app running in Azure. The folder that I am deploying is made of:
-testapp
node_modules
index.js
package.json
package-lock.json
web.config
-testapp2
node_modules
index.js
package.json
package-lock.json
The source code in ./index.js
and ./testapp2/index.js
are almost the same, only the outputs are different
const express = require("express");
const app = express();
app.get("/", (_, res)=>{
res.status(200).send("Welcome to TestApp"); // testapp2: Welcome to TestApp2
});
app.get("/test", (_, res)=>{
res.status(200).send("TestApp TestPath"); //testapp2: TestApp2 TestPath
});
const PORT = process.env.PORT || 1337;
app.listen(PORT, () => console.log(`Listening to http://localhost:${PORT}`));
My goal is to go to the domain example.com/
and then it starts the index.js
from the main folder. When I go to example.com/testapp2
then it starts the ./testapp2/index.js
and I see the welcome message from the subfolder:
"Welcome to TestApp" when I go to "/" it works
"TestApp TestPath" when I go to "/test" it works
"Welcome to TestApp2" when I go to "/testapp2" instead I get "Cannot GET /testapp2"
"TestApp2 TestPath" when I go to "/testapp2/test" instead I get "Cannot GET /testapp2/test"
I thought this problem would be solved by changing the web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<webSocket enabled="false" />
<handlers>
<add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^index.js\/debug[\/]?" />
</rule>
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>
<!-- ------------------------This is the ruled I created-------------------------- -->
<rule name="TestApp2Content" stopProcessing="true">
<match url="^testapp2"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="True"/>
</conditions>
<action type="Rewrite" url="testapp2/index.js"/>
</rule>
<!-- ----------------------------------------------------------------------------- -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="index.js"/>
</rule>
</rules>
</rewrite>
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin"/>
</hiddenSegments>
</requestFiltering>
</security>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</configuration>
But I am still getting the Cannot GET Error. I wanted to know what am I doing wrong. I am a beginner when it comes to that configuration file and the Rewrite Rules, that's why I am pretty sure that there is the error but I don't know how to write the rule properly. What am I doing wrong?
Thanks a lot for your time!