Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 尝试代理请求时出错(EconRefused)_Node.js_Reactjs_Express - Fatal编程技术网

Node.js 尝试代理请求时出错(EconRefused)

Node.js 尝试代理请求时出错(EconRefused),node.js,reactjs,express,Node.js,Reactjs,Express,这可能被标记为重复,但我查看了所有关于此主题的堆栈溢出帖子。我也在下面列出了其中的一些。关于这个话题的许多帖子也没有正确答案“勾选” 我有一个React应用程序,希望使用Express连接到节点服务器。这是我第一次这样做,所以我跟随这篇文章。我按照所有的指示去做,据我所知,我已经检查过好几次了。所以我去做了研究,看看能不能找出问题所在 当我运行npm run dev启动服务器和React应用程序时,React应用程序在我的浏览器中正确显示。但是,当我在文本框中键入我的姓名时,我的终端出现以下错误

这可能被标记为重复,但我查看了所有关于此主题的堆栈溢出帖子。我也在下面列出了其中的一些。关于这个话题的许多帖子也没有正确答案“勾选”

我有一个React应用程序,希望使用Express连接到节点服务器。这是我第一次这样做,所以我跟随这篇文章。我按照所有的指示去做,据我所知,我已经检查过好几次了。所以我去做了研究,看看能不能找出问题所在

当我运行
npm run dev
启动服务器和React应用程序时,React应用程序在我的浏览器中正确显示。但是,当我在文本框中键入我的姓名时,我的终端出现以下错误:

[HPM] Error occurred while trying to proxy request /api/greeting?name=Johnnie from localhost:3000 to http://localhost:3001/ (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
在前端的控制台中,我得到:

VM92:1 GET http://localhost:3000/api/greeting?name=Johnnie 504 (Gateway Timeout)
Uncaught (in promise) SyntaxError: Unexpected token E in JSON at position 0
Fetch failed loading: GET "http://localhost:3000/api/greeting?name=Johnnie.
我做了数小时的研究,并遵循了以下StackOverflow帖子的一些说明:

……还有更多

我还添加了一个setupProxy.js文件

这是我的package.json代码段:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "server": "node-env-run server --exec nodemon | pino-colada",
    "dev": "run-p server start"
  },
  "proxy": "https://localhost:3001/",
我的环境

REACT_APP_API_URL=http://localhost:3001
setupProxy.js

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
    app.use(createProxyMiddleware("/api/*", { target: "http://localhost:3001/" }));
};
server/index.js

const express = require('express');
const bodyParser = require('body-parser');
const pino = require('express-pino-logger')();
const PORT = 3001;

const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(pino);

app.get('/api/greeting', (req, res) => {
  const name = req.query.name || 'World';
  res.setHeader('Content-Type', 'application/json');
  res.send(JSON.stringify({ greeting: `Hello ${name}!` }));
});

app.listen(3001, () =>
  console.log('Express server is running on localhost:3001')
);
App.js中的handleSubmit函数

  handleSubmit(event) {
    event.preventDefault();
    fetch(`/api/greeting?name=${encodeURIComponent(this.state.name)}`)
      .then(response => response.json())
      .then(state => this.setState(state));
  }

我知道这个问题与服务器和应用程序不能同时运行有关,但我不知道会出什么问题。

你解决了吗?我在和sameYes斗争,我做到了。如果我没记错的话,这和我的文件结构有关。你的文件结构是什么?你的服务器代码在哪里?稍后我会查看我的代码,我不在我的计算机上,不能准确地记得你解决了它吗?我在和sameYes斗争,我做到了。如果我没记错的话,这和我的文件结构有关。你的文件结构是什么?你的服务器代码在哪里?稍后我会查看我的代码,我不在电脑上,记不清了