You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron应用调用远程方法时出现getaddrinfo ENOTFOUND 8081错误的原因

问题分析:Electron应用部署后API调用出现getaddrinfo ENOTFOUND 8081错误

基于Electron 13.6.9开发的应用,通过axios命名的ipcMain事件调用IIS(PHP)部署的API,本地测试完全正常,但部署到服务器后触发错误:Error: Error invoking remote method 'axios': Error: getaddrinfo ENOTFOUND 8081。怀疑错误与8081端口相关,但未找到对应配置,以下是相关代码及错误信息,需明确错误原因及端口关联度。


事件定义(index.js)

ipcMain.handle('axios', async (_, token = null, type, uri, body = null, headers = {}) => {
    const result = await new Promise(function (resolve, reject) {      
        if (uri.slice(-1) == '/' || uri.indexOf('/?') > 0) {
          console.error('Estimtrack Error: Please correct this url ' + uri + ' and remove the last slash "/" it will not work in production due to https cross origin problems with the api.')
        }

        var cacheHeaders = {
          'Cache-Control': 'no-store, no-cache',
          'Pragma': 'no-cache',
          'Authorization': token
        }

        var localHeaders = {
          headers: cacheHeaders,
          method: type,
          url: uri
        }
        let mergedHeaders = Object.assign({}, localHeaders, headers)

        if (body != null) {
          mergedHeaders.data = body
        }
        axios(mergedHeaders)
          .then(response => {
            resolve(response)
          }).catch(e => {
            reject(e)
        })
      })           

      return { data: result.data }
})

调用代码

import store from '../store'
import axios from 'axios'
import { ipcRenderer } from 'electron'

const estimtrackAxios = {

  install (Vue, options) {
    Vue.prototype.$estimtrackAxios = function (type, uri, body = null, headers = {}) {
      return ipcRenderer.invoke('axios', 
                                store.getters.getAccessToken, 
                                type, 
                                store.getters.getFrontOption("SERVER_API") + uri,
                                body,
                                headers)   
                        .then ((value) => {
                            return value
                        })                       
    }
  }
}
export default estimtrackAxios

错误追踪信息

http://172.17.53.43:9185/api/quirofans/mac_address/00:50:56:8f:30:19
C:\captis-pre\CaptisDeviceFront\resources\app.asar\node_modules\vue\dist\vue.runtime.common.dev.js:8699 Download the Vue Devtools extension for a better development experience:
https://github.com/vuejs/vue-devtools
C:\captis-pre\CaptisDeviceFront\resources\app.asar\node_modules\vue\dist\vue.runtime.common.dev.js:8706 You are running Vue in development mode.
Make sure to turn on production mode when deploying for production.
See more tips at https://vuejs.org/guide/deployment.html
renderer.js:15 Error: Error invoking remote method 'axios': Error: getaddrinfo ENOTFOUND 8081
    at EventEmitter.i.invoke (electron/js2c/renderer_init.js:73)
renderer.js:15 Error Error: Error invoking remote method 'axios': Error: getaddrinfo ENOTFOUND 8081
    at EventEmitter.i.invoke (electron/js2c/renderer_init.js:73)

错误原因及解决方案

1. 错误核心原因

getaddrinfo ENOTFOUND 8081表示Node.js(Electron主进程基于Node.js)将8081当作主机名而非端口号去解析,导致找不到对应的网络地址。本质是传递给axios的URL格式错误:

  • 生产环境中store.getters.getFrontOption("SERVER_API")的配置异常,可能仅返回8081,或为空值,拼接后URL变成8081/xxx这类无效格式;
  • 本地环境的SERVER_API配置是完整的(如http://172.17.53.43:9185),所以调用正常,部署后配置未同步或被错误修改。

2. 排查与修复步骤

  • 校验生产环境配置:确认store.getters.getFrontOption("SERVER_API")的返回值为完整的API基础地址(需包含协议、主机、端口,如http://你的服务器IP:9185);
  • 添加日志排查:在主进程的axios事件处理函数中,打印接收到的uri参数,确认实际传递的URL格式是否正确;
  • 增加格式校验:在调用代码中添加判断,确保SERVER_API以http://或https://开头,避免无效拼接;
  • 优化错误提示:在主进程中对传入的uri做合法性检查,若格式错误直接抛出明确提示,减少排查成本。

内容的提问来源于stack exchange,提问作者Juan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 13:27:45