首页 > 解决方案 > 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,但没有任何效果。我该如何解决?

标签: node.jsreactjsexpressaxios

解决方案


推荐阅读