-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
45 lines (38 loc) · 1.09 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
const express = require("express");
const dotenv = require("dotenv");
const httpProxy = require("http-proxy");
dotenv.config();
// Create Express Server
const app = express();
const server = require("http").createServer(app);
// Environment Variables
const PORT = process.env.PORT || 8080;
const API_SERVICE_URL = process.env.API_SERVICE_URL;
// Proxy Server Options
const options = {
target: API_SERVICE_URL,
changeOrigin: true,
ws: true,
pathRewrite: {
[`^/graphql`]: "",
},
onProxyReq: (proxyReq, req, res) => {
// Set a new authorization header for the outgoing request to the API
proxyReq.setHeader("Authorization", `Bearer ${process.env.INKEEP_API_KEY}`);
}
};
const proxy = httpProxy.createProxyServer(options);
// Handle all requests
app.all("/*", (req, res) => {
console.log("Request ALL");
proxy.web(req, res);
});
// Handle WebSocket upgrade requests
server.on("upgrade", (req, socket, head) => {
console.log("connecting");
proxy.ws(req, socket, head);
});
// Start the server
server.listen(PORT, () => {
console.log(`Starting proxy server at:${PORT}`);
});