0

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

2
  • Just typical IIS setup, a site with several applications under it. Each should use HttpPlatformHandler to forward the traffic to backend Node.js processes, halfblood.pro/… Commented Jan 26, 2024 at 4:07
  • If I understand correctly, assuming the request URL looks like mywebapp/app1/dashboard, do you want to return the index.html page in the App1 directory by setting the URL rewrite rule? Commented Feb 7, 2024 at 9:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.