Skip to content

Instantly share code, notes, and snippets.

@adaptivedev
Created May 31, 2014 05:28
Show Gist options
  • Save adaptivedev/85ad21eafb7110bc093d to your computer and use it in GitHub Desktop.
Save adaptivedev/85ad21eafb7110bc093d to your computer and use it in GitHub Desktop.
why i am getting called in "options" on the node side?
function getEvents($scope, $http) {
$http.get('http://adaptivedev.com:5432/events').
success(function(data) {
$scope.event = data;
});
}
<html ng-app>
<head>
<title>Booster</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="events.js"></script>
</head>
<body>
<div ng-controller="getEvents">
<p>Booster: {{event.booster}}</p>
<p>Role: {{event.role}}</p>
<p>Event: {{event.name}}</p>
<p>Time: {{event.time}}</p>
</div>
</body>
</html>
var express = require('express');
var events = require('./routes/events');
var app = express();
app.configure(function () {
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
});
app.get('/events', events.findAll);
app.get('/events/events/user/:uid', events.findByUid);
app.post('/events', events.addEvent);
app.get('/index.html')
var port = 5432;
app.listen(port);
console.log('Listening on port ' + port);
var mongo = require('mongodb');
var configdb = require('routes/configdb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
var server = new Server('localhost', 27017, {auto_reconnect:true});
var server_type = configdb.all.server.type;
var theDb = 'booster_db_'+ server_type;
var boosterdb = new Db(theDb, server, {w:'majority'}, {safe:true});
boosterdb.open(function(err, boosterdb) {
if(!err) {
console.log("events.js:Connected to 'booster_db_'"+configdb.all.server.type);
boosterdb.collection('events', {strict:true}, function(err, collection) {
if (err) {
//console.log("The 'events' collection doesn't exist. Creating it with sample data...");
//populateDB();
}
});
}
});
exports.findBy_id = function(req, res) {
var id = req.params.id;
console.log('Retrieving event:' + id);
boosterdb.collection('events', function(err, collection) {
console.log("events.js:findBy_id:collection");
collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
console.log("events.js:findBy_id:findOne");
res.send(item);
});
});
};
exports.findAll = function(req, res) {
console.log("events.js:findAll");
boosterdb.collection('events', function(err, collection) {
console.log("events.js:findAll:collection");
/*
collection.find(function(err, items) {
console.log("events.js:findAll:find() items.length="+items.length);
res.send(items);
});
*/
collection.find().toArray(function(err, items) {
console.log("events.js:findAll:find():toArray:items.length="+items.length);
res.send(items);
});
});
};
function compareEventInts(a,b) {
if (a.event < b.event)
return 1;
if (a.event > b.event)
return -1;
return 0;
}
function compareEventStrings(a,b) {
if (parseInt(a.event,10) < parseInt(b.event,10))
return 1;
if (parseInt(a.event,10) > parseInt(b.event,10))
return -1;
return 0;
}
/**
* get events for uid
*/
exports.findByUid = function(req, res) {
var uid = req.params.uid;
console.log("events.js:findByGidForUid:uid="+uid);
boosterdb.collection('events', function(err, collection) {
//collection.find({"uid":BSON.ObjectID(uid)}, function(err, item) {
collection.find({"uid":uid}, function(err, item) {
item.toArray(function(err, item) {
item.sort(compareEventStrings);
//item.sort(compareEventInts);
//console.log("events.js:findByGidForUid:sort:item="+item);
//res.send(item[0]);
res.send(item);
console.log("events.js:findByGidForUid:find:item.length="+item.length);
//console.log("events.js:findByGidForUid:find:item="+JSON.stringify(item));
});
});
});
};
exports.addEvent = function(req, res) {
var doer = req.body.doer;
var name = req.body.name;
var booster = req.body.booster;
var date = req.body.date;
var alarm = req.body.alarm;
console.log('events.js:addEvent:req.body=' + JSON.stringify(req.body));
boosterdb.collection('events', function(err, collection) {
collection.insert({"doer":doer, "name":name, "booster":booster, "date":date, "alarm":alarm}, {safe:true}, function(err, result) {
if (err) {
res.send({'error':'An error has occurred'});
} else {
console.log('events.js:Success:' + JSON.stringify(result[0]));
res.send(result[0]);
}
});
});
//events.addTurnyUser(req, res);
events.updateEvent(req, res);
};
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
/*--------------------------------------------------------------------------------------------------------------------*/
// Populate database with sample data -- Only used once:the first time the application is started.
// You'd typically not find this code in a real-life app, since the database would already exist.
var populateDB = function() {
var events = [
{
name:"John Doe1",
eventname:"gym",
date:new Date(),
},
{
name:"Jane Smith",
eventname:"gym",
date:new Date(),
}];
boosterdb.collection('events', function(err, collection) {
collection.insert(events, {safe:true}, function(err, result) {});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment