Initially I tried to use the router.post
and router.get
methods and have them separated in my code.
Then I opted to use router.all
, and within the same function separate POST
and GET
and use two res.render
and a common object with the viewData they both share, as it will load the same screen.
I'm wondering if this is a good approach to it and how this is usually done in node.js / express.js:
router.all('/profile', function(req, res, next) {
var viewData = {
data1: Demo.getdata(1),
data2: Demo.getdata(2),
data3: Demo.getdata(3),
data4: Demo.getdata(4)
};
if (req.method === 'POST') {
request.get({
uri: res.locals.baseUrl + '/getData',
qs: {
param1: param1
}
}, function(error, response, body) {
if (error || response.statusCode != 200) {
viewData.message = 'There was a problem!!';
}
else{
var data = JSON.parse(body);
viewData.message = 'Good job!!';
viewData.moreData= data.moreData;
}
res.render('settings/profile', viewData);
});
}
else{
res.render('settings/profile', viewData);
}
});