klandr-server/server.js

42 lines
1.0 KiB
JavaScript
Raw Normal View History

2016-09-17 02:08:42 +00:00
var express = require('express');
var app = express();
var MongoClient = require('mongodb').MongoClient, assert = require('assert');
var url = 'mongodb://localhost:27017/dat3holdetstime';
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/static'));
var mongodb;
// Connect to mongodb
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected succesfully to Mongo server");
mongodb = db;
});
var getdata = function(callback) {
var collection = mongodb.collection("klandringer");
var cursor = collection.find();
cursor.toArray(function(err, result) {
if (err) {
throw err;
} else {
var klanddata = {};
for(var i = 0; i < result.length; i++) {
klanddata[result[i]['date']] = result[i]['data'];
}
callback(klanddata);
}
});
}
app.get('/', function(req, res) {
getdata(function(result) {
res.render("klandringer", {data:result});
});
});
app.listen(35211, function () {
console.log('Server listening on port 35211!');
});