node.js - Request Graphql api running on different port from Next.js app in development mode localhost -




i'am switching existing app create-react-app next.js, seems work correctly except api end point running on node app on port 4000, can't access next.js app. followed examples in repo can't make work, in production use nginx reverse proxy no problem , i'am in dev mode . setting apollo redux followed example :with-apollo-and-redux , proxy used example with-custom-reverse-proxy

i know , making wrong, can't figure out @ moment

in initapollo.js

...     function create() {       return new apolloclient({         ssrmode: !process.browser,         networkinterface: createnetworkinterface({           uri: "/api"          })       });     } ... 

in server.js

...     const devproxy = {       "/api/": {         target: "http://localhost:4000/api/",         changeorigin: true       }     };      app       .prepare()       .then(() => {         const server = express();          if (dev && devproxy) {           const proxymiddleware = require('http-proxy-middleware')           object.keys(devproxy).foreach(function (context) {             server.use(proxymiddleware(context, devproxy[context]))           })         }          server.all("*", (req, res) => handle(req, res));          server.listen(3000, err => {           if (err) throw err;           console.log("> ready on http://localhost:3000");         });       })       .catch(ex => {         console.error(ex.stack);         process.exit(1);       }); 

ok found problem which, not related next.js or apollo client. instead it's server running on port 4000 (my graphql server). had deal cors, , express middlware express-graphql think it's not supported default .

so add following server running graphql

app.use("/api", function(req, res, next) {   res.header("access-control-allow-origin", "*");   res.header(     "access-control-allow-headers",     "content-type, authorization, content-length, x-requested-with"   );   if (req.method === "options") {     res.sendstatus(200);   } else {     next();   } }); 

and in apollo client had put absolut path api server

networkinterface: createnetworkinterface({       uri: "http://localhost:4000/api/"     }) 

that's





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -