简体   繁体   English

来自React的Express POST请求返回空主体

[英]Express POST request from React return empty body

Hello, I am working on a react/express app. 您好,我正在开发一个react / express应用。 I've been able to make a GET request from my server, however, I'm having trouble making a POST request. 我已经能够从服务器发出GET请求,但是,我在发出POST请求时遇到了麻烦。 When I do, my server is returning an empty body. 当我这样做时,我的服务器返回一个空的主体。 I have body-parser as a dependency, accepting json as content, but still no luck with the empty body. 我将body-parser作为依赖项,接受json作为内容,但是对于空的body仍然没有运气。 My code and dependencies for server/client are below. 我的代码和服务器/客户端的依赖关系如下。 Please let me know if you guys see something I don't. 如果你们看到我没看到的东西,请告诉我。

Index.js Index.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);

When I switch the order around of where my routes are declared in my index.js file, for example, declare the routes line after 例如,当我在index.js文件中声明路线的位置切换顺序时,请在以下位置声明路线

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

Line, I get an undefined as oppose to an empty body. 线,我得到一个未定义的反对一个空的身体。

POST Route 开机自检路线

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

My console.log is returning an empty object. 我的console.log返回一个空对象。 I've made the post request in postman, from my browser, curl etc. I'm getting a 200 status response back, however, the body is still empty. 我已经在邮递员中通过我的浏览器,curl等发出了发帖请求。我收到200状态回复,但是,正文仍然是空的。

Server package.json 服务器package.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"
  }
}

Client Package.json 客户端Package.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/"
}

React POST Route 反应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)
});

Any help is appreciated here figuring out why my post request is returning an empty body. 在弄清楚为什么我的帖子请求返回一个空的正文时,我们将不胜感激。

Try to remove this line: 尝试删除此行:

mode: 'no-cors'

Worked for me. 为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM