node.js - Post request method on server calling twice. Why so? -
1st time, doing expected thing. don't know why calling again , though have undefined checks over, still printing.
router.post('/:id?', function (req, res) { var id = req.params.id; console.log(id); if (id!== 'undefined') { debugger; console.log(req.method); console.log('body: ' + json.stringify(req.body)); console.log(req.body.query.tostring()); } if(req.body != 'undefined') res.render('index', { title: 'shisodia mimanshu', query: 'random query', text: 'success 111' + req.body.query }); });
output @ console:
1 post body: {"query":"hahahhahahha"} hahahhahahha post /1 200 55.494 ms - 1015 undefined post body: {} post / 500 13.445 ms - 1408
the response post method not sent reason getting 2 outputs.the server call ends once response sent client. , checking id null should changes 'undefined', 'undefined' string.
router.post('/:id?', function (req, res) { var id = req.params.id; console.log(id); if (id!= null) { debugger; console.log(req.method); console.log('body: ' + json.stringify(req.body)); console.log(req.body.query.tostring()); res.end('completed') } if(req.body != null) res.render('index', { title: 'shisodia mimanshu', query: 'random query', text: 'success 111' + req.body.query }); res.end('done') });
hope helps.
Comments
Post a Comment