Categories
React Answers

How to proxy to backend with default Next.js dev server?

Spread the love

To proxy to backend with default Next.js dev server, we configure our Next.js config with the URL rewrite.

For instance, in next.config.js, we write

module.exports = {
  async rewrites() {
    return [
      {
        source: "/api/:path*",
        destination: "http://localhost:8000/:path*",
      },
    ];
  },
};

to return an array with an object with the source path and the destination path which we redirect the source path to.

This lets us create a proxy to the back end for our Next.js app

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *