node.js respond after success insert in mongo?


I'm doing an ajax $.post to a node.js route which inserts into a mongodb collection

collection.insert(req.body.content, function () {
    return req.body.content

How do I return something so that I can do something with the response in the client callback?

For example in php I can echo.


you should have access to the request object (req in your example) and a response object. All you need to do is write to the response object.

res.writeHead(200, {
  'Content-type': 'text/plain' // or whatever content type you want
// you can use res.write also, but call end when you are done.
res.end('the text you want to send'); 

This video can help you solving your question :)
By: admin