question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

req.body undefined

See original GitHub issue

here is my code and I used postman in chrome in post verb to add json like {aaa:'bbb'} but always the result is {}

var express=require('express'),
    bodyParser = require('body-parser'),
    mongoose=require('mongoose'),
    app=express(),
    port=process.env.PORT||3000;

var jsonParser = bodyParser.json({ type: 'application/*+json' } );
var productsRouter=express.Router();


productsRouter.post('/add',jsonParser,function(req,res){
       res.send(req.body);
});
app.use('/products',productsRouter);

app.listen(port,function(){
    console.log('your server is running');
});

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
adalscommented, Feb 23, 2016

thank you very much for your reply here is my code after editing

var express=require('express'),
    bodyParser = require('body-parser'),
    mongoose=require('mongoose'),
    app=express(),
    port=process.env.PORT||3000;

var jsonParser = bodyParser.json();
var productsRouter=express.Router();


productsRouter.post('/add',jsonParser,function(req,res){
    res.send(req.body);
});
app.use('/products',productsRouter);

app.listen(port,function(){
    console.log('your server is running');
});

and I sent json {aaa:'bbb'} and result still empty {}

0reactions
adalscommented, Feb 23, 2016

thanks for your help

Read more comments on GitHub >

github_iconTop Results From Across the Web

Express.js req.body undefined - Stack Overflow
I read that this problem is caused by the lack of app.use(express.bodyParser()); but as you can see I call it before the routes....
Read more >
How to fix "req.body undefined" in Express.js
In this post we will learn how to fix the "req.body undefined" error in Express.js Framework.
Read more >
Cannot read property req body undefined in express - YouTube
codercrab #express #body-parserCannot read property req body undefined in express ||Express req body is undefined for post requests.
Read more >
node js express req.body undefined - You.com
First time using express, it seem whenever I call req.body in this application it is returning as undefined. Any ideas why? App is...
Read more >
How to fix the Express req.body undefined error - Reactgo
In this tutorial, we will learn how to fix the req.body undefined error in express. Consider, we have a /users route in our...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found