I'm trying to build a small web app as I try to learn MEAN stack web development.
I have a running MongoDB with a collection named 'contactlist', this list will then be accessed by a Node.js/Express.js server to retrieve the information into a JSON object and pass it through an angular controller.
var express = require('express');
var mongojs = require('mongojs');
var db = mongojs('contactlist', ['contactlist']);
var app = express();
const port = 80;
app.use(express.static(__dirname + "/public"));
app.get('/contactlist', function(req, res){
console.log("I received a GET request")
db.contactlist.find(function(err, docs){
console.log("Getting data from db");
console.log(docs);
res.json(docs);
});
console.log("Returned data");
});
app.listen(port,'0.0.0.0');
console.log('Server running on port '+port);
Included code for controller
var myApp = angular.module('myApp',[]);
myApp.controller('AppCtrl',['$scope','$http',function ($scope,$http){
console.log("Hello World")
console.log("Getting data from server")
$http.get('/contactList').success(function(response){
console.log("I got the data I requested");
$scope.contactList = response;
})
}]);
log from the server:
C:\Users\savila\Documents\Code\contactListApp>node server
Server running on port 80
I received a GET request
Returned data
Getting data from db
undefined
mongojs, it might be wise to instead use themongodbpackage, which is the official driver. The official driver seems to have much more thorough documentation.mongojsclaims to implement the mongodb api, but there might be slight deviances in some edge cases that could cause some frustration. Plus, a lot more people will be familiar with the official driver, so you will find it easier to get help.