我正在从监听getStaticProps
中端口5000的flask API中获取数据。我注意到获取http://127.0.0.1:5000/posts
有效,但http://localhost:5000/posts
无效。同样的问题也发生在getServerSideProps
上。但如果我从客户端获取,两个url都有效。
下面是我的代码:
import axios from "axios";
export default function SSG({ data }: { data: any }) {
return <div>SSG</div>;
}
export async function getStaticProps() {
const res = await axios.get("http://localhost:5000/posts");
return {
props: {
data: res["data"],
},
};
}
下面是错误消息:
error - AxiosError: connect ECONNREFUSED ::1:5000
at AxiosError.from (webpack-internal:///./node_modules/axios/lib/core/AxiosError.js:94:14)
at RedirectableRequest.handleRequestError (webpack-internal:///./node_modules/axios/lib/adapters/http.js:550:75)
at RedirectableRequest.emit (node:events:513:28)
at eventHandlers.<computed> (/Users/tdawg/Desktop/axios-test/node_modules/follow-redirects/index.js:14:24)
at ClientRequest.emit (node:events:513:28)
at Socket.socketErrorListener (node:_http_client:481:9)
at Socket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
port: 5000,
address: '::1',
syscall: 'connect',
code: 'ECONNREFUSED',
errno: -61
这似乎不像是一个axios的问题,因为我观察到同样的,即使与未来13的fetch
。
为什么127.0.0.1
可以工作而localhost
不能?
1条答案
按热度按时间piah890a1#
127.0.0.1
地址指定IPv4,看起来localhost
正在解析为IPv6(即address: '::1'
)。可能您的软件环境没有针对IPv6正确设置。