reactjs - 错误 (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
},
有什么办法可以修复错误吗?
解决方案
推荐阅读
- iccube - 如何设置码头设置?
- python - 熊猫:倒计时行直到出现值
- java - Case Statement with multiple conditions using hibernate criteria
- .net - .NET Core - How to include package without nuget
- javascript - 第一次后不显示 Ajax 加载屏幕
- node.js - 如何从对象数组中选择特定键的所有值
- c# - 区分 SignOutAsync 和 SlidingExpiration
- matplotlib - nbconvert 乳胶输出无法链接内部单元格
- python - Global variable resets in Python
- bash - 在 ksh/html 中检索我的 CGI 上的 query_string