-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-middleware.js
35 lines (32 loc) · 1.13 KB
/
proxy-middleware.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
const { createProxyMiddleware } = require("http-proxy-middleware");
function relayRequestHeaders(proxyReq, req) {
// Object.keys(req.headers).forEach(function(key) {
// proxyReq.setHeader(key, req.headers[key]);
// });
if (req.body) {
let bodyData = JSON.stringify(req.body);
// incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
proxyReq.setHeader("Content-Type", "application/json");
proxyReq.setHeader("Content-Length", Buffer.byteLength(bodyData));
// stream the content
proxyReq.write(bodyData);
}
}
function relayResponseHeaders(proxyRes, req, res) {
Object.keys(proxyRes.headers).forEach(function(key) {
res.append(key, proxyRes.headers[key]);
});
}
module.exports = target => {
return createProxyMiddleware(["/api", "/storage", "/sanctum"], {
target,
changeOrigin: true,
secure: false,
cookieDomainRewrite: {
"*": "localhost"
},
onProxyReq: relayRequestHeaders,
onProxyRes: relayResponseHeaders,
logLevel: "warn"
});
};