-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (28 loc) · 831 Bytes
/
index.js
File metadata and controls
33 lines (28 loc) · 831 Bytes
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
const functions = require("@google-cloud/functions-framework");
const httpProxy = require("http-proxy");
const https = require("https");
const pass_target = "https://api.openai.com";
const httpsAgent = new https.Agent();
const proxy = httpProxy.createServer({
target: pass_target,
agent: httpsAgent,
changeOrigin: true,
proxyTimeout: 30000,
});
functions.http("pass_proxy", (req, res) => {
proxy.on("error", function (err, req, res) {
res.writeHead(500, {
"Content-Type": "text/plain",
});
res.end(
`Something went wrong. And we are reporting a custom error message.`
);
});
proxy.on("proxyReq", function (proxyReq, req, res) {
if (req.body && req.complete) {
const bodyData = JSON.stringify(req.body);
proxyReq.write(bodyData);
}
});
proxy.web(req, res);
});