forked from ni/systemlink-enterprise-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (34 loc) · 984 Bytes
/
index.js
File metadata and controls
44 lines (34 loc) · 984 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
34
35
36
37
38
39
40
41
42
43
44
import express from "express";
import cors from "cors";
import { apiKey, apiServerUrl } from "./proxyConfig.js";
const app = express();
const PORT = 4000;
// Middleware
app.use(cors());
app.use(express.json());
// Simple proxy endpoint
app.all("/apiProxy/*splat", async (req, res) => {
const forwardPath = req.originalUrl.replace(/^\/apiProxy/, "");
const forwardFullUrl = `${apiServerUrl}${forwardPath}`;
const forwardReq = {
method: req.method,
headers: {
"x-ni-api-key": apiKey,
},
body: req.body,
};
try {
const upstreamResponse = await fetch(forwardFullUrl, forwardReq);
if (!upstreamResponse.ok) {
return res.status(response.status).send({});
}
const data = await upstreamResponse.json();
res.json(data);
} catch (err) {
console.error(err);
res.status(500).send({ error: "Proxy server error" });
}
});
app.listen(PORT, () => {
console.log(`Proxy server running on http://localhost:${PORT}`);
});