I am learning nodejs and express, during my studies I don’t understand various uses of req such as:
var id = req.params.id;
var wine = req.body;
With my req object I think am acessing body, params.id, but I do not understand how this works and what result I should expect. Can anyone explain this code in a simpler step by step manner so i can make sense of it ?
Below is some example code where the above is used :
exports.addDoctor = function(req,res){
var doctor = req.body;
console.log(doctor);
db.collection('doctors',function(err,collection){
collection.insert(doctor,{safe:true},function(err,result){
if (err) {
res.send({'error':'An error is occured'});
} else {
console.log('Success: ' + JSON.stringify(result[0]));
res.send(result[0]);
}
});
});
}
In the above example console.log(doctor) only returns {}. Why is this and is it because I have missed something else within the code specifically involving ‘req’?
reqis the request object. It’s documented here: http://expressjs.com/api.html#req.params