简体   繁体   English

NodeJs json post 请求 BODY 未被解析

[英]NodeJs json post request BODY not being parsed

I am trying to make a simple API in nodeJS, but I hit a wall when trying to post some data.我正在尝试在 nodeJS 中创建一个简单的 API,但是在尝试发布一些数据时我碰壁了。

This is my app.js file:这是我的 app.js 文件:

const express = require('express');
const feedRoutes = require('./routes/feed');

const app = express();

app.use(express.json());

app.use('/feed', feedRoutes)

app.listen(8080)

This is my feed.js routes file:这是我的 feed.js 路由文件:

const express = require('express');
const router = express.Router();

const feedController = require('../controllers/feed');

// GET /feed/posts
router.get('/posts', feedController.getPosts);

// POST /feed/posts
router.post('/post', feedController.createPosts)

module.exports = router

And this is my controller feed.js:这是我的控制器 feed.js:

exports.getPosts = (req, res, next) => {
  res.status(200).json({
    posts: [
      {
        title: 'First post',
        content: 'My awesome text',
      }
    ]
  })
}

exports.createPosts = (req, res, next) => {
  const {
    title,
    content,
  } = req.body

  //Create in db later

  res.status(201).json({
    post: {
      id: '1',
      title: title,
      content: content,
    },
    metadata: {
      message: 'Post was created',
    },
  })
}

From what I've read for node.js you need a body parser.根据我为 node.js 阅读的内容,您需要一个正文解析器。 Since I am using express 4.16 it is included and I thought I can just solve it with this linen app.use(express.json());由于我使用的是 express 4.16,它被包含在内,我想我可以用这个亚麻布app.use(express.json());解决它app.use(express.json());

However it looks something like this:但是它看起来像这样:

__lookupSetter__:function __lookupSetter__() { … }
constructor:function Object() { … }
hasOwnProperty:function hasOwnProperty() { … }
isPrototypeOf:function isPrototypeOf() { … }
propertyIsEnumerable:function propertyIsEnumerable() { … }
toLocaleString:function toLocaleString() { … }
toString:function toString() { … }
valueOf:function valueOf() { … }
__proto__:null

Any idea why I can't get title and content?知道为什么我无法获得标题和内容吗?

I use postman to make a request with the post method on localhost:8080/feed/post With this in the raw data section我使用邮递员在localhost:8080/feed/post使用 post 方法发出请求 在原始数据部分

{
    "title": "Look A POST!",
    "content": "Meh"
}

You can find the full code here: https://github.com/Skillvendor/node-js-api你可以在这里找到完整的代码: https : //github.com/Skillvendor/node-js-api

Edit1: This is what i see in the response: Edit1:这是我在回复中看到的:

req.body
Object {}
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}

req.body.title
undefined

I fixed the Postman json, still persists the error我修复了 Postman json,仍然存在错误

Edit2编辑2

After enabling corse(Since postman is not in the browser it seems to be considered a CORS problem, though you don't see the error sadly) Adding this solved it:启用 corse 后(由于邮递员不在浏览器中,因此它似乎被认为是 CORS 问题,尽管您没有看到该错误可悲)添加此解决了它:

app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    next();
});

You need to use bodyParser middleware.您需要使用bodyParser中间件。 Please take a look at this请看看这个

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

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