I am creating RESTful API for my application, and I am using Bodyparser for parsing the JSON data sent to my API endpoint.
app.get('/api/endpoint', function(req,res) {
console.log(req.body);
console.log(req.body.path.split('/'));
res.status(200).end();
});
I had configured the bodyparser in below way,
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
The problem is when I send this,
var params = {
path: '/dir1/dir2/dir3/pptdir'
};
It throws an error, TypeError: Cannot call method 'split' of undefined
After this error both the console.log statements work fine and prints the below output.
{
path: '/dir1/dir2/dir3/pptdir'
}
[ '', 'dir1', 'dir2', 'dir3', 'pptdir' ]