0

我有以下服务器,它在本地运行良好。当我尝试部署到 Heroku 时,代理似乎可以工作,但静态文件只给了我 404。

const Koa = require("koa");
const proxy = require("koa-proxies");
const serve = require("koa-static");
const path = require("path");
const koaHistorify = require("koa-historify");

const port = process.env.PORT || 80;
const secure = process.env.RUN_INSECURE || true;
const apiUrl = ... Api Url ...

const fileLocation = path.join(__dirname, "../dist");
const indexPath = path.join( fileLocation, "index.html" );

const coreProxyConfig = {
  target: `${apiUrl}`,
  changeOrigin: true,
  secure,
};

class PureServer {
  constructor() {
    this.app = new Koa();
    this.app.use((ctx, next) => {
        const req = ctx.request;
        if (req.header["x-forwarded-proto"] !== "https") {
          ctx.redirect(`https://${req.header["host"]}${req.url}`);
        } else next();
    });
    this.app.use(proxy("/login", coreProxyConfig));
    this.app.use(proxy("/logout", coreProxyConfig));
    this.app.use(proxy("/userinfo", coreProxyConfig));
    this.app.use(proxy("/callback", coreProxyConfig));
    this.app
      .use(serve(fileLocation))
      .use(koaHistorify(indexPath));
  }
  start() {
    this.app.listen(port, ()=>{
      console.info("Server started "+port);
    });
  }
}

const server = new PureServer();
server.start();

为什么 Heroku 不使用 Koa-static?

4

0 回答 0