I have a general static web app (SPA), and inside it, multiple folders representing more static web apps (SPA). The folder structure looks like this basically
index.html
main.js
main.css
App1/
index.html
main.js
main.css
App2/
index.html
main.js
main.css
In node.js, I have a server file which supports SPA from the root, or from each App folder, using this
import express from 'express';
import * as http from 'http';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const httpServer = http.createServer(app);
app.use(express.static(path.join(__dirname, 'public')));
httpServer.listen(process.env.PORT || 3001, function () {
const address = httpServer.address();
if (typeof address === 'string') {
console.log('Sample Web App started at http://%s', address);
} else {
const host = address?.address;
const port = address?.port;
console.log('Sample Web App started at http://%s:%s', host, port);
}
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', path.dirname(req.path), 'index.html'));
});
This part specifically handles the nested apps res.sendFile(path.join(__dirname, 'public', path.dirname(req.path), 'index.html'));.
How this works is, lets say the request URL looks like https://mywebapp/app1/dashboard, then path.dirname(req.path) will convert to /app1 and use that value, so it returns the index.html file from the app1 folder.
The problem now is that I need to convert this node.js code into a IIS web.config file. So far I have this
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="SPA" stopProcessing="true">
<match url="^(?!.*(.js|.css|.png|.jpg|.ico|.svg)).*$" />
<conditions logicalGrouping="MatchAll">
</conditions>
<action type="Rewrite" url="/" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But this only supports SPA from the root. It doesn't work with nested folders. How can I do this?
Thanks