node.js respond after success insert in mongo?
-
11-10-2019 - |
Pergunta
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.
Solução
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');
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow