How do I send data from server to the client using nodejs?
So basically I call this function by clicking a button
javascript
function createStuff(tid) {
$.ajax({
type: "POST",
url: '/create/',
data: {tid: tid}
success: function(id) {
doStuff(id);
},
error: function(jqXHR, textstatus, errorThrown) {
alert('text status ' + textstatus + ', err ' + errorThrown);
}
});
};
This then handles the request
server
exports.create = function(req, res) {
new Stuff({
content: "random stuff"
}).save(function(err, stuff) {
Otherstuff.update({_id: req.body.tid}, {$push: {stuffes: stuff}}, {upsert: true}, function(err, mvar) {
res.redirect(301, '/' + req.body.tid);
});
});
};
But I need to send along the newly created stuff._id with the res.redirect. The thing is I don't want to send it as res.redirect(301, '/' + req.body.tid + '/' + stuff._id) because I would have to do a whole new router which doesn't seem flexible. Also, when I do this request, the web page doesn't reload, which is just like I want it.
I tried using res.send(stuff._id), but I could only do it once (because the connection closes after it it seems). I'm using the following libraries: mongoose, jquery, express