我一直在尝试向 Postman 上的服务器发送发布请求。但我一直收到 400 Bad Request 错误。错误描述为“由于语法错误,无法满足请求”。但是,我的 JSON 主体没有任何错误的语法,其他类似的问题没有涵盖语法正确时发生的此错误。
这就是标题的样子:
发生这种情况的原因可能是什么?
编辑: 这是服务器端的代码:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const articleRoutes = express.Router();
const PORT = 4000;
let Article = require('./article.model');
app.use(cors());
app.use(bodyParser.json());
mongoose.connect('mongodb://127.0.0.1:27017/articles', { useNewUrlParser: true, useUnifiedTopology: true });
const connection = mongoose.connection;
connection.once('open', function() {
console.log("MongoDB database connection established successfully");
});
articleRoutes.route("/content-management-system/add").post((req, res) => {
let article = new Article(req.body);
console.log(article);
article.save()
.then(article => {
res.status(200).json({'article': 'article saved successfully'});
})
.catch(err => {
res.status(400).send('adding new article failed');
});
});
app.use('/', articleRoutes);
app.listen(PORT, function() {
console.log("Server is running on Port: " + PORT);
});