I need help. I am sending json data to node server. The node server uses RESTify for its API. I'm having trouble getting req.body.name from the body of the published data.
Published data contains json body. In it, I have keys such as name, date, address, email address, etc.
I want to get the name from json body. I am trying to make req.body.name , but it does not work.
I also included server.use(restify.bodyParser()); and does not work.
I can req.params.name and assign a value. But if I POST json data like: {'food': 'ice cream', 'drink' : 'coke'} , I get undefined. However, if I do req.body , I get the full json body message. I want to be able to specifically get an item like "drink", and have this show on the .log console.
var restify = require('restify'); var server = restify.createServer({ name: 'Hello World!', version: '1.0.0' }); server.use(restify.acceptParser(server.acceptable)); server.use(restify.jsonp()); server.use(restify.bodyParser({ mapParams: false })); server.post('/locations/:name', function(req, res, next){ var name_value = req.params.name; res.contentType = 'json'; console.log(req.params.name_value); console.log(req.body.test); }); server.listen(8080, function () { console.log('%s listening at %s', server.name, server.url); });
Danny BoyWonder
source share