My ExpressJS backend was running smoothly with hardcoded data, but when I integrated MongoDB into the system, my requests for data started timing out. I added a record to a collection using the command prompt:
> db
stackmailer
> db.sites.find()
{
"_id" : ObjectId("55ef5c1a7f6857848b7149b7"),
"title" : "Stack Overflow",
"icon" : "http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png"
}
The MongoDB server is hosted at localhost:27017
2015-09-08T23:58:51.394+0200 I CONTROL [initandlisten] MongoDB starting : pid=6836 port=27017 dbpath=C:\data\db\ 64-bit host=JEROEN-LAPTOP
2015-09-08T23:58:51.422+0200 I NETWORK [initandlisten] waiting for connections on port 27017
2015-09-08T23:58:54.760+0200 I NETWORK [initandlisten] connection accepted from 127.0.0.1:51658 #1 (1 connection now open)
To read the data, I have this code:
var db = require('mongoskin').db('localhost:27017/stackmailer');
router.get('/', function (req, res, next) {
db.collection('sites').find().toArray(function (err, result) {
if (err) {
console.log(err);
throw err;
}
console.log("found sites!");
console.log(result);
res.send(result);
});
}
And in AngularJS, it's called like this:
StackExchangeService.getSites().then(function(data) {
$scope.data.sites = data;
});
angular.module('StackMailer.Services', [])
.factory('StackExchangeService', function($q, $http) {
var service = {
getSites: function() {
return getData('/sites');
}
};
function getData(url)
{
var d = $q.defer();
$http.get(url, ({ timeout: 10000 }))
.then(function(response, status) {
console.log("successfully retrieved data");
d.resolve(response.data);
}, function(response, status) {
console.log("couldn't retrieve data " + status);
d.reject(response);
});
return d.promise;
};
return service;
});
However, after the specified timeout, I see "couldn't retrieve data 0" in the console. The console.log
calls inside router.get()
don't display any output.
I suspect there might be an issue with the MongoDB connection, even though everything seems to be set up correctly.
When examining the ExpressJS output, I notice the following for 3 requests:
GET / 304 5.335 ms - -
GET /css/stackmailer.css 304 4.784 ms - -
GET /js/stackmailer.js 304 1.250 ms - -
GET /sites 500 479.902 ms - 1424
GET /tags 304 2.303 ms - -
GET /css/stackmailer.css 304 0.937 ms - -
GET / 304 1.028 ms - -
GET /css/stackmailer.css 304 1.231 ms - -
GET /js/stackmailer.js 304 1.608 ms - -
GET /tags 304 1.156 ms - -
GET /sites - - ms - -
GET / 304 2.988 ms - -
GET /css/stackmailer.css 304 4.508 ms - -
GET /js/stackmailer.js 304 2.022 ms - -
GET /tags 304 1.336 ms - -
GET /sites - - ms - -
The first request to /sites
returns HTTP 500 and then times out. This error message appears in the console log:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Interestingly, when I wrap the db.collection()
call in a try-catch block and attempt to log the error, the HTTP 500 error disappears, but no output is shown either.
Any thoughts on what could be causing this issue?