ashes999
ashes999

Reputation: 1324

How to host NextJS app with an Express server backend on same localhost port

I am trying to host my NextJS app built with React on the same localhost port as my express api-server backend.

Inside my express server api setting I have set my api server to listen on:

http://localhost:3000/graphql

how can I set it up such that when I go to localhost:3000 I can see the frontend as well.

Right now I am getting an error:

Cannot GET /

I know I cannot use "proxy":"localhost:3000/graphql" how can I do this using NextJS?

Upvotes: 2

Views: 1302

Answers (1)

Danila
Danila

Reputation: 18476

You could use rewrites key in next.config:

module.exports = {
  async rewrites() {
    return [
      process.env.NODE_ENV === 'development' && {
        source: '/graphql',
        destination: 'localhost:3000/graphql',
      },
    ].filter(Boolean);
  },
}

Just start you Next.js on any other port and all request to /graphql will be redirected to localhost:3000/graphql

Upvotes: 1

Related Questions