Computerlucaworld Computerlucaworld - 3 months ago 10
Node.js Question

Express router doesn't work

Why this router doesn't work? I executed the route but it doesn't return any response.
Someone can explain to me why this code doesn't work?

var mongo = require('mongodb').MongoClient;
var express = require('express');
var app = express();
app.get ('/',function(req,resp){
mongo.connect(url,function(err,db){
if(err) throw err;
var cursor = db.collection('link').find();

cursor.each(
function(doc) {
var count = 0;
var result = '';
if(count ==0) count++; result = "<!doctype html><html><head>URLShortener Home page</head><body><h1>In this page you can see the urls have already been shortened using this service<ul>";
console.log(doc);
if(doc !==null && count >0)
{
result += "<li>old URL: " + doc.old+"</li>";
}
else{
result += "</ul></body></html>";
resp.end(result);
}


});

});
})

Answer

try this

cursor.toArray(function(err, doc){ console.log(doc); })