I'm brand new to node.js (less than one hour).
I'm trying to bring up a simple http server that will read a mongodb collection and print the data to the browser window.
So far, I have:
var http = require ("http")
var mongodb = require('mongodb');
http.createServer(function(request, response) {
var server = new mongodb.Server("127.0.0.1", 27107, {});
response.writeHead(200, {"Content-Type": "text/plain"});
response.write('Collection Data:<br>')
new mongodb.Db('testdb', server, {}).open(function (error, client) {
if (error) throw error;
var collection = new mongodb.Collection(client, 'test_coll');
collection.find({}, {limit:100}).each(function(err, doc) {
if (doc != null) {
console.dir(doc.text);
response.write(doc.text)
}
});
response.write("some stuff")
response.end();
});
}).listen(8080)
This puts the text of the collection items onto the console, but not to the browser window. I think this is because the response object is not in scope in the .each callback? Have I structured this the wrong way?