-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
47 lines (40 loc) · 1.6 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const express = require("express");
require("dotenv").config();
const app = express();
const PORT = parseInt(process.env.PORT, 10); // Set server port
const { createProxyMiddleware } = require("http-proxy-middleware");
// Set 60 Seconds timeout for proxy requests
const TIMEOUT = 60 * 1000;
app.use(express.static("dist"));
// Set up api proxy
const toolhuntApiProxy = () => {
return createProxyMiddleware({
target: process.env.VITE_FLASK_BACKEND_URL,
changeOrigin: true,
logger: console,
proxyTimeout: TIMEOUT,
timeout: TIMEOUT,
onProxyRes: (proxyRes, req, res) => {
// log original request and proxied request info
const exchange = `[${req.method}] [${proxyRes.statusCode}] ${req.path} -> ${proxyRes.req.protocol}//${proxyRes.req.host}${proxyRes.req.path}`;
console.log(exchange); // [GET] [200] / -> http://www.example.com
const cleanup = (err) => {
// cleanup event listeners to allow clean garbage collection
proxyRes.removeListener("error", cleanup);
proxyRes.removeListener("close", cleanup);
res.removeListener("error", cleanup);
res.removeListener("close", cleanup);
// destroy all source streams to propagate the caught event backward
req.destroy(err);
proxyRes.destroy(err);
};
proxyRes.once("error", cleanup);
proxyRes.once("close", cleanup);
res.once("error", cleanup);
res.once("close", cleanup);
},
});
};
app.use("/api", toolhuntApiProxy());
app.use("/openapi.json", toolhuntApiProxy());
app.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));