title: Send data from server(nodejs) to client(ajax request)
body: <p>How do I send data from server to the client using nodejs?</p>
  
  <p>So basically I call this function by clicking a button</p>
  
  <p>javascript</p>
  
  <pre><code>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);
          }
      });
  };
  </code></pre>
  
  <p>This then handles the request</p>
  
  <p>server</p>
  
  <pre><code>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);
          });
      });
  };
  </code></pre>
  
  <p>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.</p>
  
  <p>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 </p>