[英]Next.JS: Can't call Internal API in getInitialProps. Got Error: read ECONNRESET
當我在 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 { }
}
但是如果我使用外部 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 { }
}
如果我在 componentDidMount 中調用 API,它在兩種情況下都可以正常工作,但是在 getinitialProps 中,我無法處理 Express 服務器上的內部 API。
請幫忙! 我的代碼有問題嗎? 我從過去幾個小時開始搜索,但無法解決。
您可以從請求中提取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.