Notwithstanding Precautions, Misadventure
    Have ideas to improve npm?Join in the discussion! »

    next-http-proxy-middleware
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.10 • Public • Published

    Next.js HTTP Proxy Middleware

    All Contributors

    HTTP Proxy middleware available in API Middleware provided by Next.js.

    Installation

    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

    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

    • The key is a regular expression to match url, and the matched url string is replaced with value.

    Example

    // pages/[...all].ts
    ...
    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`
          pathRewrite: {
            '^/api/new': '/v2',
            '^/api': '',
          },
        })
        : res.status(404).send(null)
    );

    Contributors

    Thanks goes to these wonderful people (emoji key):


    Denny Lim

    🐛 💻

    Kristian Tryggestad

    🐛 💻

    Gunnlaugur Thor Briem

    💻 🤔

    Otto von Wesendonk

    🛡️

    Daniel Silva

    🤔

    Yann Pringault

    💻

    This project follows the all-contributors specification. Contributions of any kind welcome!

    Install

    npm i next-http-proxy-middleware

    DownloadsWeekly Downloads

    2,395

    Version

    1.0.10

    License

    MIT

    Unpacked Size

    17.2 kB

    Total Files

    16

    Last publish

    Collaborators

    • avatar