-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
32 lines (29 loc) · 905 Bytes
/
index.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
const path = require("path");
const express = require("express");
const PORT = 3000;
const {createProxyMiddleware} = require("http-proxy-middleware");
const app = express();
app.use(express.static(path.join(__dirname, "public")));
app.use(
"/v1/",
createProxyMiddleware({
target: "https://api.openai.com",
changeOrigin: true,
onProxyReq: (proxyReq, req, res) => {
console.log(req.originalUrl);
proxyReq.setHeader("Authorization", `Bearer ${process.env.API_KEY}`);
},
onProxyRes: (proxyRes, req, res) => {
proxyRes.headers["Access-Control-Allow-Origin"] = "*";
proxyRes.headers["Access-Control-Allow-Headers"] =
"Content-Type,Content-Length, Authorization, Accept,X-Requested-With";
},
})
);
app
.listen(PORT, () => {
console.log(`server running on http://localhost:${PORT}`);
})
.on("error", (err) => {
console.log(err);
});