node.js - POST net::ERR_EMPTY_RESPONSE axios
问题描述
我在前端有这个(反应)
const { siteLocation, Services, date, cnum } = this.state;
const selections = {
siteLocation,
Services,
date,
cnum,
};
axios
.post('http://localhost:3001/updateServices')
.then(() =>
console.log("updating"),
window.location = "/services"
)
.catch(function(error) {
alert(error)
window.location = "/"
})
};
这在后端:
const path = require('path')
var express = require('express')
var app = express()
const cors = require('cors')
const bodyParser = require('body-parser')
const https = require('https')
const http = require('http')
const port = process.env.PORT
const sslport = process.env.SSLPORT
//set up paths for express
const publicPath = path.join(__dirname, '../client/build')
//set up static directory
app.use(express.static(publicPath))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cors({origin: true, credentials: true}));
app.use(bodyParser.json())
app.use(express.json())
app.use(testRouter)
// Handles any requests that don't match the ones above
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '../client/build/index.html'));
});
app.post('/updateServices', (req, res) => {
console.log(req.body.Services)
res.send({message: "success"})
})
const localServers = process.env.LOCALDEV || '';
if (localServers == 'YES') {
https.createServer({
key,
cert
}, app).listen(sslport || 3001, () => {
console.log('listening on port ' + sslport)
});
}
http.createServer(app).listen(port || 80, () => {
console.log('listening on port ' + port)
});
当我尝试从 axios 向 node.js 后端发出 POST 请求时,我在控制台中收到此错误:
POST http://localhost:3001/updateServices net::ERR_EMPTY_RESPONSE
我以前从未遇到过这个问题。网络设置搞砸了可能是一个问题(我正在网上阅读)?我正在使用 cors、express、body-parser 和 axios,但没有任何效果。我该如何解决?
解决方案
推荐阅读
- c# - 统一如何将一个输入字段文本或标签文本从一个输入字段文本或标签文本传输到另一类输入字段文本或标签文本?
- python - 使用 lambda 进行 Python 排序
- sql - 如何不在单个 BigQuery 表中重复记录,并从单个表中获取每月总计
- c - 如何解决 C 语言 CPU 调度模拟程序中的分段错误(核心转储)错误?
- ios - 快速动画表格视图单元格
- regex - 捕获组中的正则表达式替换
- python - 使用 Python 的 SQLALCHEMY 连接到 Cloud SQL (PostgreSQL) 时遇到问题
- c - 反向打印华氏-摄氏表
- r - 在 R 中重新格式化数据框
- git - 删除远程分支是否也会删除本地远程跟踪分支?