node.js respond after success insert in mongo?
-
11-10-2019 - |
문제
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');
제휴하지 않습니다 StackOverflow