【发布时间】:2021-07-24 13:36:47
【问题描述】:
我正在尝试从客户端应用程序发送对象数据,但它不返回服务器端只返回一个空对象。我没有错误,并且在控制台中提交后。
客户端使用这个:
const url = `http://localhost:5000/addUser`;
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(userData)
}).then(res => console.log(res))
我使用的服务器端:
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
require("dotenv").config()
const port = process.env.PORT || 5000;
const { MongoClient } = require('mongodb');
const app = express()
app.use(cors())
app.use(express.json())
app.use(bodyParser.json({
limit: '50mb',
parameterLimit: 100000
}))
app.post('/addUser', (req, res) =>{
const newUser = req.body;
console.log('added new user', newUser)
})
服务器端 package.json
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mongodb": "^3.6.10",
"nodemon": "^2.0.10"
}
}
【问题讨论】:
标签: node.js reactjs mongodb express