簡體   English   中英

來自React的Express POST請求返回空主體

[英]Express POST request from React return empty body

您好,我正在開發一個react / express應用。 我已經能夠從服務器發出GET請求,但是,我在發出POST請求時遇到了麻煩。 當我這樣做時,我的服務器返回一個空的主體。 我將body-parser作為依賴項,接受json作為內容,但是對於空的body仍然沒有運氣。 我的代碼和服務器/客戶端的依賴關系如下。 如果你們看到我沒看到的東西,請告訴我。

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);

例如,當我在index.js文件中聲明路線的位置切換順序時,請在以下位置聲明路線

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

線,我得到一個未定義的反對一個空的身體。

開機自檢路線

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

我的console.log返回一個空對象。 我已經在郵遞員中通過我的瀏覽器,curl等發出了發帖請求。我收到200狀態回復,但是,正文仍然是空的。

服務器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"
  }
}

客戶端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/"
}

反應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)
});

在弄清楚為什么我的帖子請求返回一個空的正文時,我們將不勝感激。

嘗試刪除此行:

mode: 'no-cors'

為我工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM