Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom proxy doesn't work on getInitialProps #4719

Closed
hoangdv1995 opened this issue Jul 3, 2018 · 1 comment
Closed

Custom proxy doesn't work on getInitialProps #4719

hoangdv1995 opened this issue Jul 3, 2018 · 1 comment

Comments

@hoangdv1995
Copy link

Hi all,
I want to customize http://localhost:3000/api/ to http://api.example/api/. I've referenced https://github.com/zeit/next.js/tree/canary/examples/with-custom-reverse-proxy but it does not work on getInitialProps.

// server.js
const next = require('next');
const routes = require('./routes');
const proxyMiddleware = require('http-proxy-middleware');

const devProxy = {
  '/api/**': {
    target: 'http://api.example/',
    changeOrigin: true,
  }
};

const port = 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({dir: '.', dev});
const handle = routes.getRequestHandler(app);

const express = require('express');
app.prepare()
  .then(() => {
    const server = express();
    if (dev && devProxy) {
      Object.keys(devProxy).forEach(function (context) {
        server.use(proxyMiddleware(context, devProxy[context]))
      });
    }
    server.all('*', (req, res) => handle(req, res));
    server.listen(port, (err) => {
      if (err) throw err;
      console.log(`> Ready on http://localhost:${port}`)
    })
  })
  .catch(err => {
    console.log('An error occurred, unable to start the server');
    console.log(err);
  })
;

Thanks!

@timneutkens
Copy link
Member

timneutkens commented Jul 3, 2018

Questions should be posted on https://spectrum.chat/next-js.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants