Shayan Khan Shayan Khan - 5 months ago 11
Node.js Question

Express router "res" object has to take me to other pages

See the example below:

var apiRouter = express.Router();

apiRouter.post('/api/postAgree', function(req, res, next){
userModel.findOneAndUpdate(
{profileID: req.session.facebookProfileId},
{$push:{postsAgreed: req.query.postID}},
{safe: true, upsert: true},

function(err, model) {
if (err){
console.log(err);
}
}
)


Now, the MongoDB operation is already done and I want to stay on the same page.

Will I be doing this:

res.render('theSamePageIamOn', {foo:bar});


I know this works but it seems like it is a very inefficient way of doing it.

So my question really is: If I have a button on a page which makes an API call but I want to stay on the same page, how will I do that? The res.(options) function sort of is made like it has to take me to other pages

Answer

Thanks to @robertklep and @GiladArtzi - it should be an AJAX call and the response should be in the form of:

res.json()

Then the response can be handled by the frontend using other tools like: Angular