【问题标题】:Express POST request from React return empty body来自 React 的 Express POST 请求返回空正文
【发布时间】:2018-01-14 05:13:56
【问题描述】:

您好,我正在开发一个 react/express 应用程序。我已经能够从我的服务器发出 GET 请求,但是,我在发出 POST 请求时遇到了麻烦。当我这样做时,我的服务器返回一个空的身体。我有 body-parser 作为依赖项,接受 json 作为内容,但空体仍然没有运气。我的服务器/客户端的代码和依赖项如下。如果你们看到我没有看到的东西,请告诉我。

索引.js

 const express = require('express');
 const path = require('path');
 const app = express();
 const bodyParser = require('body-parser');

//routes
 require('./routes/sendMessageToEmail')(app);
 require('./routes/helloWorld')(app);

 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({ extended: true }));

app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', (req, res) => {
 res.sendFile(path.join(__dirname+'/client/build/index.html'));
});

const port = process.env.PORT || 1111;
app.listen(port);

例如,当我在 index.js 文件中切换路由声明的顺序时,在

之后声明路由行
 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({ extended: true }));

行,我得到一个 undefined 作为空体的对立面。

发布路线

   module.exports = app => {
     app.post('/api/message', (req, res) => {
      console.log('>>>>>>>>>> ', req.body);
      res.send(req.body)
     });
    }

我的 console.log 返回一个空对象。我已经通过我的浏览器、curl 等在邮递员中发出了发布请求。我收到了 200 状态响应,但是,正文仍然是空的。

服务器包.json

{
  "name": "Blog",
  "version": "1.0.0",
  "engines": {
    "node": "6.11.1"
  },
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "server": "nodemon index.js",
    "client": "npm run start --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client\"",
    "heroku-postbuild": "cd client && npm install --only=dev && npm install && npm run build"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.17.2",
    "concurrently": "^3.5.0",
    "express": "^4.15.3"
  }
}

客户端包.json

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "axios": "^0.16.2",
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "react-iframe": "^1.0.7",
    "react-redux": "^5.0.5",
    "react-router-dom": "^4.1.2",
    "react-scroll": "^1.5.4",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0",
    "semantic-ui-react": "^0.71.3"
  },
  "devDependencies": {
    "react-scripts": "1.0.10"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:1111/"
}

反应 POST 路由

import axios from 'axios';

const sendToExpress = (input) => {
 return fetch('/api/message', {
   mode: 'no-cors',
   method: 'POST',
   headers: {
   Accept: 'application/json',
   'Content-Type': 'application/json',
   },
   body: JSON.stringify({message: 'asdfafa;k'})
   //body: JSON.stringify({"message": input})
  })
}


//ACTION CREATORS

export const sendInputToServer = (input) => ({
 type: 'SEND_INPUT_TO_SERVER', payload: sendToExpress(input)
});

感谢任何帮助,了解为什么我的帖子请求返回一个空的正文。

【问题讨论】:

  • 你的路由肯定需要在正文解析器中间件之后。此外,在您的路线中,您可能还想使用 res.json() 而不是 result.send 因为您的客户期望接收 json
  • 嘿,我换了路线,也尝试了 res.json(),但仍然没有雪茄。您可以在我的代码中看到任何其他可能是帖子正文返回空的问题?

标签: javascript node.js reactjs express cors


【解决方案1】:

尝试删除此行:

mode: 'no-cors'

为我工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-13
    • 2019-06-26
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多