首页 > 解决方案 > 错误 (ECONNRESET) 将请求发布到快速服务器。反应快递应用

问题描述

我正在使用 express 上的服务器创建一个反应应用程序。客户端发送一个 post 请求:

            try {
                const response = await fetch('/astronauts', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data),
                })
            } catch (e) {
                console.log(e);
            }

服务器处理程序:

app.post('/astronauts', async (req, res) => {
    try {
        await writeFileAsync('data', 'astronauts.json', req.body);

        res.status(200);
    } catch (e) {
        res.status(500).json({
            message: 'Server error'
        })
    }
    
})

结果,在浏览器中我收到 504(网关超时)错误。在服务器上:

Error occurred while trying to proxy request /astronauts from localhost:4120 to http://localhost:4320/ (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)

我猜为什么会这样。服务器或客户端过早关闭连接。

Webpack 开发服务器设置:

devServer: {
    historyApiFallback: true,
    port: 4120,
    open: isWindows ? 'chrome' : 'google-chrome',
    proxy: {
      '/astronauts': 'http://localhost:4320/',
      "secure": false,
      "changeOrigin": true
    },
  },

package.json 设置的一部分:

"scripts": {
    "server": "nodemon server/index.js",
    "devFront": "cross-env NODE_ENV=development webpack --mode development",
    "dev-server": "cross-env NODE_ENV=development webpack-dev-server --mode development --open",
    "dev": "concurrently --kill-others-on-fail \"npm run server\" \"npm run dev-server\"",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false webpack -p"
  },
  "proxy": {
    "/astronauts": "http://localhost:4320/",
    "secure": false,
    "changeOrigin": true
  },

有什么办法可以修复错误吗?

标签: reactjsexpress

解决方案


推荐阅读