Last active
June 22, 2020 20:26
-
-
Save Starefossen/8003095 to your computer and use it in GitHub Desktop.
Stream MongoDB documents from Node.JS server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict" | |
http = require 'http' | |
MongoClient = require('mongodb').MongoClient | |
stringify = require('JSONStream').stringify | |
db = null | |
server = http.createServer (req, res) -> | |
cursor = db.collection('turer').find() | |
stream = cursor.stream() | |
res.writeHead 200, 'Content-Type': 'application/json; charset=utf-8' | |
op = '{"documents":[' | |
cl = '],"count":0,"total":0}' | |
stream.pipe(stringify(op, ',', cl)).pipe(res) | |
MongoClient.connect 'mongodb://localhost:27017/test', (err, database) -> | |
db = database | |
server.listen 8080, -> console.log 'Server is listening on port 4000...' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment