无法将请求/推送/身份验证从localhost:3000代理到http:// localhost:5000(ECONNREFUSED)

时间:2017-07-28 07:24:06

标签: reactjs

我正在尝试使用reactJS和推送器创建聊天应用,我收到此错误 -

  

无法从localhost:3000代理请求/推送/身份验证   http://localhost:5000(ECONNREFUSED)

在package.json文件中的

我将代理设置为 -

"proxy": "http://localhost:5000"

我的localhost127.0.0.1文件中定义为/etc/hosts 我还使用netstat检查了端口可用性,但这些似乎都是正确的。有人可以帮忙吗?

16 个答案:

答案 0 :(得分:4)

我在我的React App中有一个相同的问题,我通过在package.json文件中的端口号后面添加“ /”来解决它(现在是:“ proxy”:“ http://localhost:5000/”)

答案 1 :(得分:2)

在你的服务器package.json中添加--ignore client到你的" start"或者"服务器"脚本。所以它看起来像这样:

 "scripts": {
 "start": "node index.js",
 "server": "nodemon index.js --ignore client"
 }

答案 2 :(得分:2)

我在Mac计算机中遇到了类似的问题。我将/home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require': libMagickCore-6.Q16.so.2: cannot open shared object file: No such file or directory - /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rmagick-2.15.0/lib/RMagick2.so (LoadError) from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rmagick-2.15.0/lib/rmagick_internal.rb:12:in `<top (required)>' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rmagick-2.15.0/lib/rmagick.rb:1:in `<top (required)>' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/site_ruby/2.2.0/bundler/runtime.rb:81:in `require' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/site_ruby/2.2.0/bundler/runtime.rb:81:in `block (2 levels) in require' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/site_ruby/2.2.0/bundler/runtime.rb:76:in `each' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/site_ruby/2.2.0/bundler/runtime.rb:76:in `block in require' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/site_ruby/2.2.0/bundler/runtime.rb:65:in `each' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/site_ruby/2.2.0/bundler/runtime.rb:65:in `require' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/site_ruby/2.2.0/bundler.rb:114:in `require' from /home/effectech/workspace/hotel_management/config/application.rb:7:in `<top (required)>' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:78:in `require' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:78:in `block in server' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `tap' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `server' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>' from /home/effectech/workspace/hotel_management/bin/rails:9:in `require' from /home/effectech/workspace/hotel_management/bin/rails:9:in `<top (required)>' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load' from /home/effectech/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>' from /home/effectech/workspace/hotel_management/bin/spring:15:in `require' from /home/effectech/workspace/hotel_management/bin/spring:15:in `<top (required)>' from bin/rails:3:in `load' from bin/rails:3:in `<main>' 更改为localhost,这对我有用。

对于Windows:

127.0.0.1

对于Mac:

"proxy": {
  "/auth/google": {
    "target": "localhost:5000"
  }
}

答案 3 :(得分:2)

在您的节点模块中包括

{
...
  "proxy": "http://127.0.0.1:5000"
}

...在哪里……只是意味着您应该将代理ip附加到它。

此外,如果您使用的是axios,则执行axios.post('api/users')会起作用,而不是axios.post('/api/users')

答案 4 :(得分:1)

在服务器目录中

npm install --save http-proxy-middleware

然后创建一个具有以下名称的文件:setupProxy.js 在客户端react文件夹的src目录中

enter image description here

然后添加以下内容

enter image description here

const proxy = require("http-proxy-middleware");
module.exports = function(app) {
  app.use(proxy("/api/**", { // https://github.com/chimurai/http-proxy-middleware
    target: "http://localhost:5000",
    secure: false
  }));
};

在代理配置中,请确保您匹配任何路径 不仅带有双** *

注意:您在其他任何地方都不需要此代理 就是这样

注意:删除package.json中的所有其他代理设置 有关更多信息,请检查此reference

答案 5 :(得分:1)

对于那些使用 Docker 的人,如果您的 docker-compose.yml 看起来像:

services:
  app:
    ...
    depends_on:
      - api
    ports:
      - 3000:xxxx
    ...
  api:
    ...
    ports:
      - 5000:xxxx
    ...

然后我们应该将代理 URL 设置为

  "proxy": "http://host.docker.internal:5000"

答案 6 :(得分:0)

如果您无法通过telnet连接到端口5000上的localhost(如果您没有安装telnet,则可以下载并使用PuttY),这意味着服务器未运行。

如果您使用的是Windows计算机,请转到package.json以获取在端口5000上运行的服务器并更改此行:

,Andy "Blank,

对此:

"start": "./node_modules/.bin/concurrently \"./node_modules/.bin/nodemon\" \"npm run client\"",

观看构建消息,您应该看到类似于以下内容的内容:

"start": "./node_modules/.bin/concurrently \"npm run server\" \"npm run client\"",

答案 7 :(得分:0)

我有类似的问题。 问题是服务器正在侦听ipv6 :: 1地址 并且代理连接到ipv4 127.0.0.1

我将两个地址从localhost更改为127.0.0.1

答案 8 :(得分:0)

我认为服务器无法正常运行,为此,您应同时运行客户端和服务器,并在 package.json 文件

中添加以下步骤

1)同时安装

npm install concurrently --save

2)配置客户端和服务器

"server": "nodemon server.js",
"client": "npm start --prefix client"

3)同时配置

"dev": "concurrently "npm run server" "npm run client""

答案 9 :(得分:0)

使用 “代理”:“ http://localhost:PORT_NUMBER/ 在package.json中

以及在axios后端调用路由中,例如 使用 axios.get(“ api / user / getinfo”)代替 axios.get(“ / api / user / getinfo”);

答案 10 :(得分:0)

我的问题是试图在docker容器打开的情况下运行我的react项目。

更改端口或关闭容器。

答案 11 :(得分:0)

在我的情况下,问题是我一直用错误的名称访问PORT,我使用的是PORT而不是SERVER_PORT,这是我正确的环境变量名称。因此,此问题意味着您的代码有问题,在我的情况下,服务器应在其上运行的端口未定义。

答案 12 :(得分:0)

我认为您尚未启动后端服务器。尝试同时启动后端服务器和前端服务器。只需在后端和前端同时运行 void read_adc_Voltage() { int arr[100]; int arr1[100]; int max = arr[0]; arr[0]=0; int i,j=0; int count = 6; for (i=1;i<count;i++) { var=(ain1Buff[sampleCounter]); voltage=var*((float)3.3/(float)4095); arr[j] = voltage; if(arr[j] > max) { max = arr[j]; } j++; } sprintf(data1,"%.2f",max); LCD_String_xy(1,1,data1); sampleCounter++; if(sampleCounter==6) { sampleCounter=0; } }

答案 13 :(得分:0)

如果您不打算在服务器端同时使用,则只需分别运行每个前端和后端,这样服务器端应首先运行而客户端应最后运行。

答案 14 :(得分:0)

localhost 更改为 [::1] 解决了我的问题。

取自此处https://forum.vuejs.org/t/proxy-error-with-vue-config-js-and-axios/110632/4?u=mahmoodvcs

答案 15 :(得分:-1)

这与 create-react-app 的默认设置有关。

我从 Github Issue 找到了解决方案。阅读 danielmahon 于 2018 年 3 月 15 日的回复

"proxy": {
    "/api": {
      "target": "https://localhost:5002",
      "secure": false
    }
  },