Skip to content

Commit ccf2c1f

Browse files
committed
Create proxy instance for each request
1 parent 2453215 commit ccf2c1f

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/index.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ export interface NextHttpProxyMiddlewareOptions extends ServerOptions {
77
}
88

99
/**
10-
* @see https://www.npmjs.com/package/http-proxy
10+
* Please refer to the following links for the specification document for HTTP.
11+
* @see https://tools.ietf.org/html/rfc7231
12+
* @see https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
1113
*/
12-
const proxy: httpProxy = httpProxy.createProxy();
14+
const hasRequestBodyMethods: string[] = ["HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "PATCH"];
1315

1416
/**
1517
* If pattern information matching the input url information is found in the `pathRewrite` array,
@@ -22,7 +24,7 @@ export const rewritePath = (
2224
pathRewrite: NextHttpProxyMiddlewareOptions['pathRewrite']
2325
) => {
2426
if(Array.isArray(pathRewrite)){
25-
for (let item of pathRewrite) {
27+
for (const item of pathRewrite) {
2628
const {
2729
patternStr,
2830
replaceStr
@@ -35,7 +37,7 @@ export const rewritePath = (
3537
} else {
3638
console.warn('[next-http-proxy-middleware] Use array instead of object for \`pathRewrite\` value '
3739
+ '(related issue: https://github.com/stegano/next-http-proxy-middleware/issues/39)');
38-
for (let patternStr in pathRewrite) {
40+
for (const patternStr in pathRewrite) {
3941
const pattern = RegExp(patternStr);
4042
const path = pathRewrite[patternStr];
4143
if (pattern.test(url as string)) {
@@ -61,20 +63,19 @@ const httpProxyMiddleware = async (
6163
new Promise((resolve, reject) => {
6264
const { pathRewrite, onProxyInit, ...serverOptions } = httpProxyOptions;
6365

66+
/**
67+
* @see https://www.npmjs.com/package/http-proxy
68+
*/
69+
const proxy: httpProxy = httpProxy.createProxy();
70+
6471
if(typeof onProxyInit === 'function') {
6572
onProxyInit(proxy);
6673
}
6774

6875
if (pathRewrite) {
6976
req.url = rewritePath(req.url as string, pathRewrite);
7077
}
71-
72-
/**
73-
* Please refer to the following links for the specification document for HTTP.
74-
* @see https://tools.ietf.org/html/rfc7231
75-
* @see https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
76-
*/
77-
const hasRequestBodyMethods: string[] = ["HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "PATCH"];
78+
7879
if (hasRequestBodyMethods.indexOf(req.method as string) >= 0 && typeof req.body === "object") {
7980
req.body = JSON.stringify(req.body);
8081
}

0 commit comments

Comments
 (0)