Piping mongoose document to Node.js response stream
- 16 Jul 2017: Post was created (diff)
You can keep the mongoose documents as streams by calling cursor()
and piping directly to the response stream of the HTTP server. In this example I’m using it in an Express.js router route definition. Since Node.js streams expect stream data to be of type String
or Buffer
, the JavaScript object needs to be stringified first using a transform
function. JSON.stringify
can be used directly which helps in making this a nice pipeline.
router.get('/', (req, res, next) => {
Model.find()
.cursor({transform: JSON.stringify})
.pipe(res.type('json'))
})
References
- https://stackoverflow.com/questions/20058614/stream-from-a-mongodb-cursor-to-express-response-in-node-js/45124486#45124486
If you have any comments or feedback, please send me an e-mail. (stig at stigok dotcom).
Did you find any typos, incorrect information, or have something to add? Then please propose a change to this post.