Skip to content

Create proxy instance for each request #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 12, 2022
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Next.js HTTP Proxy Middleware
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-13-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-12-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

HTTP Proxy middleware available in API Middleware provided by Next.js.
Expand Down
23 changes: 12 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ export interface NextHttpProxyMiddlewareOptions extends ServerOptions {
}

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

/**
* If pattern information matching the input url information is found in the `pathRewrite` array,
Expand All @@ -22,7 +24,7 @@ export const rewritePath = (
pathRewrite: NextHttpProxyMiddlewareOptions['pathRewrite']
) => {
if(Array.isArray(pathRewrite)){
for (let item of pathRewrite) {
for (const item of pathRewrite) {
const {
patternStr,
replaceStr
Expand All @@ -35,7 +37,7 @@ export const rewritePath = (
} else {
console.warn('[next-http-proxy-middleware] Use array instead of object for \`pathRewrite\` value '
+ '(related issue: https://github.com/stegano/next-http-proxy-middleware/issues/39)');
for (let patternStr in pathRewrite) {
for (const patternStr in pathRewrite) {
const pattern = RegExp(patternStr);
const path = pathRewrite[patternStr];
if (pattern.test(url as string)) {
Expand All @@ -61,20 +63,19 @@ const httpProxyMiddleware = async (
new Promise((resolve, reject) => {
const { pathRewrite, onProxyInit, ...serverOptions } = httpProxyOptions;

/**
* @see https://www.npmjs.com/package/http-proxy
*/
const proxy: httpProxy = httpProxy.createProxy();

if(typeof onProxyInit === 'function') {
onProxyInit(proxy);
}

if (pathRewrite) {
req.url = rewritePath(req.url as string, pathRewrite);
}

/**
* Please refer to the following links for the specification document for HTTP.
* @see https://tools.ietf.org/html/rfc7231
* @see https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
*/
const hasRequestBodyMethods: string[] = ["HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "PATCH"];

if (hasRequestBodyMethods.indexOf(req.method as string) >= 0 && typeof req.body === "object") {
req.body = JSON.stringify(req.body);
}
Expand Down