[Fixed] Empy req.body in NodeJs, Express


I’m trying to send a json to my nodeJs app through POST Method in body.
For that I’m using POSTMAN to create the request, with the proper consnt-type header and body JSON Rows. Tho the message back is "OK" in console the req.body is {} empty.
Would you have an idea what’s wrong in my code?

const bodyParser = require('body-parser');
const { Client } = require('pg');
const express = require('express');
const app = express();

// create application/json parser
const jsonParser = bodyParser.json()

// create application/x-www-form-urlencoded parser
const urlencodedParser = bodyParser.urlencoded({ extended: false })

const hostname = '';
const port = 3000;

const dbSchema = 'public';

const client = new Client({
    user: 'postgres',
    host: 'localhost',
    database: 'postgres',
    password: '123123',
    port: 5432,


/* =========== Some Initialize staff =============== */

// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }))

    extended: true

app.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);

/* =========== FROM HERE =============== */

app.post('/post-test', urlencodedParser, (req, res) => {
    console.log('Got body:', req.body);

    res.status(200).send('Get Ready for something awesome!\n');

enter image description here
enter image description here
enter image description here


You should use app.use(bodyParser.json());, in your code const jsonParser = bodyParser.json() this is not used.

Update: Or you can apply jsonParser middleware directly to the post route:

app.post("/post-test", jsonParser, (req, res) => {
  console.log("Got body:", req.body);
  res.json({ ...req.body });

Leave a Reply

(*) Required, Your email will not be published