【发布时间】:2023-03-04 03:20:01
【问题描述】:
我目前正在将我现有的应用程序从 create-react-app 切换到 next.js, 除了我的 api 端点在端口 4000 上的另一个节点应用程序上运行之外,一切似乎都正常工作,我无法从我的 next.js 应用程序访问它。 我按照 repo 中的示例进行操作,但无法使其正常工作, 在生产中,我使用 nginx 作为反向代理没有问题,但我处于开发模式。 为了使用 Redux 设置 Apollo,我遵循了这个例子:with-apollo-and-redux,对于代理,我使用了这个例子 with-custom-reverse-proxy
我知道,我做错了,我现在想不通
在 initApollo.js 中
...
function create() {
return new ApolloClient({
ssrMode: !process.browser,
networkInterface: createNetworkInterface({
uri: "/api"
})
});
}
...
在 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);
});
【问题讨论】:
-
如果有人能提供教程或文章的链接也很好
标签: node.js express react-apollo apollo-client next.js