2020-10-20 23:05:58 +00:00
|
|
|
import proxyServer from "http-proxy"
|
2020-11-05 22:45:58 +00:00
|
|
|
import { HttpCode } from "../common/http"
|
2020-10-20 23:05:58 +00:00
|
|
|
|
|
|
|
export const proxy = proxyServer.createProxyServer({})
|
2020-11-05 22:45:58 +00:00
|
|
|
|
2020-10-20 23:05:58 +00:00
|
|
|
proxy.on("error", (error, _, res) => {
|
|
|
|
res.writeHead(HttpCode.ServerError)
|
|
|
|
res.end(error.message)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Intercept the response to rewrite absolute redirects against the base path.
|
2021-02-05 07:31:18 +00:00
|
|
|
// Is disabled when the request has no base path which means /absproxy is in use.
|
2020-10-20 23:05:58 +00:00
|
|
|
proxy.on("proxyRes", (res, req) => {
|
|
|
|
if (res.headers.location && res.headers.location.startsWith("/") && (req as any).base) {
|
|
|
|
res.headers.location = (req as any).base + res.headers.location
|
|
|
|
}
|
|
|
|
})
|