
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
next-http-proxy-middleware
Advanced tools
HTTP Proxy middleware available in API Middleware provided by Next.js.
Please try the solutions below before using this library. 😀
Next.js
Rewrites(recommended)// next.config.js
async rewrites() {
return [
{
source: "/api/:path*",
destination: "http://example.com/api/:path*",
},
];
},
Http-Proxy
next-http-proxy-middleware
is implemented using http-proxy
internally. Since the implementation is not complicated, it is recommended to try http-proxy
directly.
// pages/api/[...all].ts
import httpProxy from "http-proxy";
export const config = {
api: {
// Enable `externalResolver` option in Next.js
externalResolver: true,
bodyParser: false,
},
};
export default (req, res) =>
new Promise((resolve, reject) => {
const proxy: httpProxy = httpProxy.createProxy();
proxy.once("proxyRes", resolve).once("error", reject).web(req, res, {
changeOrigin: true,
target: process.env.NEXT_PUBLIC_API_PROXY_URL,
});
});
The easiest way to install next-http-proxy-middleware
is with npm.
npm install next-http-proxy-middleware
Alternately, download the source.
git clone https://github.com/stegano/next-http-proxy-middleware.git
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
optionpathRewrite.patternStr
replace the URL string with the value pathRewrite.replaceStr
.onProxyInit
optionYou can access the http-proxy
instance using the onProxyInit
option. See the example below.
import type { NextApiRequest, NextApiResponse } from "next";
import type { NextHttpProxyMiddlewareOptions } from "next-http-proxy-middleware";
import httpProxyMiddleware from "next-http-proxy-middleware";
const handleProxyInit: NextHttpProxyMiddlewareOptions["onProxyInit"] = (proxy) => {
/**
* Check the list of bindable events in the `http-proxy` specification.
* @see https://www.npmjs.com/package/http-proxy#listening-for-proxy-events
*/
proxy.on("proxyReq", (proxyReq, req, res) => {
// ...
});
proxy.on("proxyRes", (proxyRes, req, res) => {
// ...
});
};
export default async (req: NextApiRequest, res: NextApiResponse) =>
httpProxyMiddleware(req, res, {
target: "http://example.com",
onProxyInit: handleProxyInit,
});
Refer to the following for how to use Next.js API Middleware
// pages/api/[...all].ts
import type { NextApiRequest, NextApiResponse } from "next";
import httpProxyMiddleware from "next-http-proxy-middleware";
const isDevelopment = process.env.NODE_ENV !== "production";
export const config = {
api: {
// Enable `externalResolver` option in Next.js
externalResolver: true,
},
}
export default (req: NextApiRequest, res: NextApiResponse) => (
isDevelopment
? httpProxyMiddleware(req, res, {
// You can use the `http-proxy` option
target: "https://www.example.com",
// In addition, you can use the `pathRewrite` option provided by `next-http-proxy-middleware`
pathRewrite: [{
patternStr: "^/api/new",
replaceStr: "/v2"
}, {
patternStr: "^/api",
replaceStr: ""
}],
})
: res.status(404).send(null)
);
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.
multipart/form-data
multipart/form-data
, refer to the Issues below
react-render-state-hook
is a React hook that enables declarative management of components based on data processing states. It facilitates straightforward state management and data sharing among multiple components in a React.Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
Nextjs HTTP Proxy Middleware
The npm package next-http-proxy-middleware receives a total of 120,168 weekly downloads. As such, next-http-proxy-middleware popularity was classified as popular.
We found that next-http-proxy-middleware demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.