-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
71 lines (62 loc) · 1.91 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// @ts-check
const express = require('express');
const config = require('./config');
const proxy = require('http-proxy-middleware');
const bodyParser = require('body-parser');
const queryString = require('querystring');
// Create proxy instance
const proxyInstance = getProxy();
const app = express();
app.use('/', proxyInstance);
// Optional: Support special POST bodies - requires restreaming (see below)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
function getProxy() {
return proxy(getProxyConfig());
}
/**
* @returns {import('http-proxy-middleware').Config}
*/
function getProxyConfig() {
return {
target: config.destination,
changeOrigin: true,
followRedirects: true,
secure: true,
/**
* @param {import('http').ClientRequest} proxyReq
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
*/
onProxyReq: (proxyReq, req, res) => {
/**
* @type {null | undefined | object}
*/
// @ts-ignore
const body = req.body;
// Restream parsed body before proxying
// https://github.com/http-party/node-http-proxy/blob/master/examples/middleware/bodyDecoder-middleware.js
if (!body || !Object.keys(body).length) {
return;
}
const contentType = proxyReq.getHeader('Content-Type');
let contentTypeStr = Array.isArray(contentType) ? contentType[0] : contentType.toString();
// Grab 'application/x-www-form-urlencoded' out of 'application/x-www-form-urlencoded; charset=utf-8'
contentTypeStr = contentTypeStr.match(/^([^;]*)/)[1];
let bodyData;
if (contentTypeStr === 'application/json') {
bodyData = JSON.stringify(body);
}
if (contentTypeStr === 'application/x-www-form-urlencoded') {
bodyData = queryString.stringify(body);
}
if (bodyData) {
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
}
}
};
}
module.exports = {
proxy: app
}