Reactjs 向express服务器发送请求时出错(EconReset)。快速反应应用程序
我正在使用express上的服务器创建react应用程序。 客户端发送post请求:Reactjs 向express服务器发送请求时出错(EconReset)。快速反应应用程序,reactjs,express,Reactjs,Express,我正在使用express上的服务器创建react应用程序。 客户端发送post请求: try { const response = await fetch('/astronauts', { method: 'POST', headers: { 'Content-Type': 'application/json'
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)
我猜这是为什么。服务器或客户端过早关闭连接
网页包开发服务器设置:
devServer: {
historyApiFallback: true,
port: 4120,
open: isWindows ? 'chrome' : 'google-chrome',
proxy: {
'/astronauts': 'http://localhost:4320/',
"secure": false,
"changeOrigin": true
},
},
"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
},
package.json设置的一部分:
devServer: {
historyApiFallback: true,
port: 4120,
open: isWindows ? 'chrome' : 'google-chrome',
proxy: {
'/astronauts': 'http://localhost:4320/',
"secure": false,
"changeOrigin": true
},
},
"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
},
有什么方法可以修复错误吗?应该是
“/antominates”:http://localhost:4320/astronouts“,
?它是否应该是”“/宇航员”:”http://localhost:4320/astronouts“,?