Static assets losing prefix
dami-i opened this issue · 1 comments
Description
I'm not sure if this is a bug or I'm doing something wrong, but I have an Express server serving a static site over port 5400. It behaves as it should when accessed through its own port, but when I try to access it through the gateway route (:8080/app), the static assets (js and css) lose their reference and a 404 error is raised. I tried adding a <base>
tag inside index.html but had no success. Is this an expected behavior? Is there a resource I could use to get around this problem?
Expected behavior (screenshot from the original port)
index.html and assets folder should be in the same folder level
Obtained behavior (screenshot from the proxy port)
Assets folder is going up one level and not respecting /app
route defined in the proxy.
Gateway code (succinct version)
import fastGateway from "fast-gateway";
const gateway = fastGateway({
routes: [
{ // API
prefix: "/api/v1",
target: "http://localhost:3000",
},
{ // View
prefix: "/app",
target: "http://localhost:5400",
},
],
});
gateway.start(8080);
Express code (port 5400)
app.use(express.static(__dirname + "/dist"));
app.get("/", (req, res) => {
res.sendFile(path.resolve(__dirname, "dist", "index.html"));
});
/dist folder structure (the folder that is being served)
/dist
/assets
index.8b3d6e23.js
index.d813aab2.css
svelte.d72399d3.png
favicon.ico
index.html
Hi @dami-i, thanks for reaching out. This is not a bug, please be aware that when serving content behind a gateway, your public links should reflect the prefix under which your service is being exposed, in this case: /app
The prefix is not included when I see the errors on your browser. To fix this issue, you have two options:
- Make your static assets reference aware of the public URL, which now includes a prefix:
/app
- Use the fast-gateway "default destination" approach to serve your static files, see:
Line 34 in ec63b1a
Please let me know,
Regards