由于某种原因(我无法找出原因),bodyParser无法解析来自我的api的请求(标题和内容)。它们似乎是undefined
app.js
const EXPRESS = require('express');
const FEED_ROUTES = require('./routes/feed');
const BODY_PARSER = require('body-parser');
const APP = EXPRESS();
APP.use(BODY_PARSER.json());
APP.use(BODY_PARSER.urlencoded({ extended: false }));
APP.use('/feed', FEED_ROUTES);
APP.listen(8080);
控制器js
exports.getPosts = (req, res, next) => {
res
.status(200)
.json({ posts: [{ title: 'test', content: 'This is a post ' }] });
};
exports.createPost = (req, res, next) => {
const title = req.body.title;
const content = req.body.content;
console.log(title);
res.status(201).json({
message: 'Post created succesfully!',
post: { id: new Date().toISOString(), title: title, content: content }
});
};
POSTMAN打印此:
{
"message": "Post created succesfully!",
"post": {
"id": "2019-07-09T00:24:57.129Z"
}
}