[英]Next.JS: Can't call Internal API in getInitialProps. Got Error: read ECONNRESET
When I use internal API route in getInitialProps I get this error.当我在 getInitialProps 中使用内部 API 路由时,出现此错误。
Error: read ECONNRESET
at _errnoException (util.js:1003:13)
at TCP.onread (net.js:620:25)
static async getInitialProps({ reduxStore }) {
const res = await Axios.get("/api/recent/1");
await reduxStore.dispatch({ type: LIST, payload: res.data });
return { }
}
But If I use external API server, it works fine.但是如果我使用外部 API 服务器,它工作正常。
static async getInitialProps({ reduxStore }) {
const res = await Axios.get("http://abc.herokuapp.com/api/recent/1");
await reduxStore.dispatch({ type: LIST, payload: res.data });
return { }
}
If I call API in componentDidMount, it works fine in both cases, but in getinitialProps I couldn't handle internal API which are on my Express server.如果我在 componentDidMount 中调用 API,它在两种情况下都可以正常工作,但是在 getinitialProps 中,我无法处理 Express 服务器上的内部 API。
Please help!请帮忙! Is there a problem in my code?
我的代码有问题吗? I am searching from past couple of hours but couldn't solve it.
我从过去几个小时开始搜索,但无法解决。
You can extract the baseUrl
from the request.您可以从请求中提取
baseUrl
。
async getInitialProps({ req }) {
const protocol = req.headers['x-forwarded-proto'] || 'http'
const baseUrl = req ? `${protocol}://${req.headers.host}` : ''
const res = await fetch(baseUrl + '/api/recent/1')
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.