簡體   English   中英

為什么 axios 試圖連接端口 80 而不是 8080?

[英]Why is axios trying to connect on port 80 and not 8080?

我在 Windows 10 機器上運行帶有vue前端的 Node/Express 后端。 我確實安裝iisnode ,因為我認為我會使用 IIS 為server.js提供服務,但目前我正在開發中,並希望將 IIS 排除在外。

但是,當我對網站進行生產構建並運行node./dist/server.js時,Node/Express 服務器在port 8080上運行,並且主頁加載正常。

當我 go 到進行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,在那里定義端口,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM