admin管理员组

文章数量:1134715

I have a public-facing node express server and a private python FastAPI server running on the same machine. I'd like to use a proxy to expose a particular route on my FastAPI server through my express server, but am facing issues writing relative paths for things like static content (ex. css).

I would like to be on the page /foo which contains a link to static/styles.css. This should send a request for /foo/static/styles.css. The proxy would receive this and go to http://127.0.0.1:8000/bar/static/styles.css which would get picked up by FastAPI and go to the /bar router, whereby it uses the /static route and serves styles.css.

Instead, the page is linking to /static/styles.css, dropping the /foo, which then 404's because it's not picked up by the proxy.

Folder structure:

├───express_server
│       server.js
│
└───fastapi_server
    │   main.py
    │
    ├───static
    │       styles.css
    │
    └───templates
            index.html
// server.js
const express = require("express");
const { createProxyMiddleware } = require("http-proxy-middleware");

const app = express();

app.use(
    "/foo",
    createProxyMiddleware({
        target: "http://127.0.0.1:8000/bar",
        changeOrigin: true,
        pathRewrite: { "^/foo": "" },
        logger: console,
    })
);

app.listen(3030, () => {
    console.log("Express server running on http://127.0.0.1:3030");
});
# main.py
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.routing import APIRouter
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

app = FastAPI()

templates = Jinja2Templates(directory="templates")

bar_router = APIRouter(prefix="/bar")

# app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount(bar_router.prefix + "/static", StaticFiles(directory="static"), name="static")


@bar_router.get("/", response_class=HTMLResponse)
async def bar_home(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})

# EDITED IN THIS ROUTE
@bar_router.get("", response_class=HTMLResponse)
async def bar_home(request: Request):
    return RedirectResponse(str(request.url) + "/", status_code=302)


app.include_router(bar_router)
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <link rel="stylesheet" href="static/styles.css">
</head>
<body>
    <h1>bar</h1>

    <a href="foo/static/styles.css">link A</a>
    <a href="static/styles.css">link B</a>
    <a href="/static/styles.css">link C</a>
    <a href="//static/styles.css">link D</a>
    <a href="./static/styles.css">link E</a>
    <a href=".//static/styles.css">link F</a>
</body>
</html>
/* styles.css */
body {
    background-color: pink;
}

Side note: Originally I wanted to use the commented line bar_router.mount to define /static relative to my /bar. Per this discussion it seems that that's not possible, so this appending method is needed to replicate that behavior.

Of the links I put on the page, only link A which hard-codes the /foo endpoint doesn't 404. I'd like my FastAPI server to not need to know about my express configuration, and simply serve its content relative to the current page.

Is there some configuration that can be applied to express or FastAPI or another way of writing the html to allow this separation of concerns?

本文标签: How can I make relative links work behind a proxy using express and FastAPIStack Overflow