Installations
npm install next-http-proxy-middleware
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>=10.0.0
Node Version
20.3.0
NPM Version
9.8.1
Score
97.9
Supply Chain
99.5
Quality
75.9
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
validate.email 🚀
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Download Statistics
Total Downloads
6,013,653
Last Day
11,216
Last Week
58,699
Last Month
248,785
Last Year
2,309,462
Bundle Size
22.88 kB
Minified
8.10 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.2.6
Package Id
next-http-proxy-middleware@1.2.6
Unpacked Size
28.37 kB
Size
8.11 kB
File Count
16
NPM Version
9.8.1
Node Version
20.3.0
Published on
Oct 08, 2023
Total Downloads
Cumulative downloads
Total Downloads
6,013,653
Last Day
0.5%
11,216
Compared to previous day
Last Week
6.9%
58,699
Compared to previous week
Last Month
37.2%
248,785
Compared to previous month
Last Year
11.1%
2,309,462
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Next.js HTTP Proxy Middleware
HTTP Proxy middleware available in API Middleware provided by Next.js.
⭐️ Before using
Please try the solutions below before using this library. 😀
Try using Next.js
Rewrites(recommended)
- This function is supported by Next.js. No additional libraries need to be installed!
1// next.config.js 2async rewrites() { 3 return [ 4 { 5 source: "/api/:path*", 6 destination: "http://example.com/api/:path*", 7 }, 8 ]; 9},
Try using Http-Proxy
next-http-proxy-middleware
is implemented usinghttp-proxy
internally. Since the implementation is not complicated, it is recommended to tryhttp-proxy
directly.1// pages/api/[...all].ts 2import httpProxy from "http-proxy"; 3 4export const config = { 5 api: { 6 // Enable `externalResolver` option in Next.js 7 externalResolver: true, 8 bodyParser: false, 9 }, 10}; 11 12export default (req, res) => 13 new Promise((resolve, reject) => { 14 const proxy: httpProxy = httpProxy.createProxy(); 15 proxy.once("proxyRes", resolve).once("error", reject).web(req, res, { 16 changeOrigin: true, 17 target: process.env.NEXT_PUBLIC_API_PROXY_URL, 18 }); 19 });
Installation
The easiest way to install next-http-proxy-middleware
is with npm.
1npm install next-http-proxy-middleware
Alternately, download the source.
1git clone https://github.com/stegano/next-http-proxy-middleware.git
Features
This middleware is implemented using the http-proxy
library. You can use the existing options provided by http-proxy
. And you can rewrite the api path using pathRewrite
, an additional option provided by this middleware.
pathRewrite
option
- Replaces URL information matching the pattern with another string.
- Priority is determined in the order entered in the array.
- If the request URL matches the pattern
pathRewrite.patternStr
replace the URL string with the valuepathRewrite.replaceStr
.
onProxyInit
option
-
You can access the
http-proxy
instance using theonProxyInit
option. See the example below.1import type { NextApiRequest, NextApiResponse } from "next"; 2import type { NextHttpProxyMiddlewareOptions } from "next-http-proxy-middleware"; 3import httpProxyMiddleware from "next-http-proxy-middleware"; 4 5const handleProxyInit: NextHttpProxyMiddlewareOptions["onProxyInit"] = (proxy) => { 6 /** 7 * Check the list of bindable events in the `http-proxy` specification. 8 * @see https://www.npmjs.com/package/http-proxy#listening-for-proxy-events 9 */ 10 proxy.on("proxyReq", (proxyReq, req, res) => { 11 // ... 12 }); 13 proxy.on("proxyRes", (proxyRes, req, res) => { 14 // ... 15 }); 16}; 17 18export default async (req: NextApiRequest, res: NextApiResponse) => 19 httpProxyMiddleware(req, res, { 20 target: "http://example.com", 21 onProxyInit: handleProxyInit, 22 });
Example
-
Refer to the following for how to use Next.js API Middleware
1// pages/api/[...all].ts 2import type { NextApiRequest, NextApiResponse } from "next"; 3import httpProxyMiddleware from "next-http-proxy-middleware"; 4 5const isDevelopment = process.env.NODE_ENV !== "production"; 6 7export const config = { 8 api: { 9 // Enable `externalResolver` option in Next.js 10 externalResolver: true, 11 }, 12} 13 14export default (req: NextApiRequest, res: NextApiResponse) => ( 15 isDevelopment 16 ? httpProxyMiddleware(req, res, { 17 // You can use the `http-proxy` option 18 target: "https://www.example.com", 19 // In addition, you can use the `pathRewrite` option provided by `next-http-proxy-middleware` 20 pathRewrite: [{ 21 patternStr: "^/api/new", 22 replaceStr: "/v2" 23 }, { 24 patternStr: "^/api", 25 replaceStr: "" 26 }], 27 }) 28 : res.status(404).send(null) 29);
externalResolver
is an explicit flag that tells the server that this route is being handled by an external resolver. Enabling this option disables warnings for unresolved requests.
Using multipart/form-data
- If you are using the
multipart/form-data
, refer to the Issues below
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!

No vulnerabilities found.

No security vulnerabilities found.