【问题标题】:Why is axios trying to connect on port 80 and not 8080?为什么 axios 尝试连接端口 80 而不是 8080?
【发布时间】:2021-02-26 02:56:07
【问题描述】:

我在 Windows 10 机器上运行带有 vue 前端的 Node/Express 后端。我确实安装了iisnode,因为我认为我会使用 IIS 为server.js 提供服务,但目前我正在开发中,希望将 IIS 排除在外。

但是,当我对网站进行生产构建并运行 node ./dist/server.js 时,Node/Express 服务器在 port 8080 上运行并且主页加载正常。

当我转到进行axios 调用的产品页面时,我收到以下错误:

Error: connect ECONNREFUSED 127.0.0.1:80
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1137:16) {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 80,
  config: {
    url: '/api/int/v1/product/10800',
    method: 'get',
    headers: {
      Accept: 'application/json, text/plain, */*',
      'User-Agent': 'axios/0.21.1'
    },
...... // more stuff here
isAxiosError: true //<== is an axios issue?

我不明白它为什么要尝试连接端口 80 而不是直接访问 http://localhost:8080/api/int/v1/product/10800

server.js 这样做:

const PORT = process.env.PORT || 8080;
server.listen(PORT, () => {
    console.log(`Server is listening on port ${PORT}`);
});

我什至在像这样运行构建脚本时设置了port:

"build:server": "cross-env PORT=8080 NODE_ENV=production webpack --config build/webpack.server.config.js"

在console 日志中甚至写着Server is listening on port 8080

【问题讨论】:

  • 使用 axios BASE_URL 配置并在那里定义端口。
  • @EduardoSousa 谢谢你解决了它:)
  • 太棒了,发布为答案,这样每个有相同问题的人都能立即得到答案。

标签: node.js windows express axios


【解决方案1】:

您需要配置 axios BASE_URL,在那里定义端口例如:

import axios from 'axios'
axios.defaults.baseURL = 'http://localhost:8080';

【讨论】:

    猜你喜欢
    • 2012-05-14
    • 2015-01-06
    • 2019-05-26
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 2017-09-09
    相关资源
    最近更新 更多