简体   繁体   中英

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.

This is my app.js file:

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:

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:

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

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

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

You can find the full code here: https://github.com/Skillvendor/node-js-api

Edit1: This is what i see in the response:

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

req.body.title
undefined

I fixed the Postman json, still persists the error

Edit2

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:

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. Please take a look at this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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