Gathering detailed insights and metrics for next-http-proxy-middleware
Gathering detailed insights and metrics for next-http-proxy-middleware
Gathering detailed insights and metrics for next-http-proxy-middleware
Gathering detailed insights and metrics for next-http-proxy-middleware
npm install next-http-proxy-middleware
Typescript
Module System
Min. Node Version
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
HTTP Proxy middleware available in API Middleware provided by Next.js.
Please try the solutions below before using this library. 😀
Next.js
Rewrites(recommended)1// next.config.js 2async rewrites() { 3 return [ 4 { 5 source: "/api/:path*", 6 destination: "http://example.com/api/:path*", 7 }, 8 ]; 9},
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.
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 });
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
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.
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 });
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.
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!
No vulnerabilities found.
No security vulnerabilities found.