Using body-parser in express route specific got empty request body
See original GitHub issueI tried to apply the the body parser into a specific router, and I always get empty request body.
the following is my code:
var express = require('express');
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json({ type: 'application/*+json'});
var router = express.Router();
router.post('/', jsonParser, function(req, res) {
if(!req.body || req.body.length === 0) {
console.log('request body not found');
return res.sendStatus(400);
}
var user = req.body;
console.log('request body : ' + JSON.stringify(user));
});
});
module.exports = router;
The same request was tried with the express app with both generic and express route specific as in the example here https://github.com/expressjs/body-parser#examples and it works fine.
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
req.body empty on posts - node.js - Stack Overflow
Whenever I make a post in nodejs using express and body-parser req.body is an empty object. var express = require('express') var bodyParser = ......
Read more >Express body-parser middleware
Node.js body parsing middleware. Parse incoming request bodies in a middleware before your handlers, available under the req.body property. Note As ...
Read more >req.body is empty in POST requests : r/node - Reddit
Hi! I know this is basically a noob question but please bear with me. I noticed that POST requests on my node server...
Read more >express router post body empty - Industry Today
For this tutorial we're going to use the express.Router middleware as it allows us to group the route handlers for a particular part...
Read more >express request body is undefined - You.com | The AI Search ...
You must make sure that you define all configurations BEFORE defining routes. If you do so, you can continue to use express.bodyParser() ....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Right, but you see, you are sending the header
Content-Type: application/json
, but in your code above, you are settingtype: 'application/*+json'
.application/json
does not matchapplication/*+json
, which is why nothing is happening. You want to just remove thetype
option all together unless there is a specific reason you have that there.👍