-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
77 lines (61 loc) · 1.99 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var mongoose = require('mongoose');
var path = require('path');
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
var ErrorHandlr = require('errorhandlr').engage()
var VdbDataSchema = mongoose.Schema({
key: String,
data: String
});
mongoose.connect('mongodb://localhost/vdb');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function(){
console.log('database connected');
});
var VdbDataModel = mongoose.model('VdbData', VdbDataSchema);
var uniqueIndex = 100000000000;
VdbDataModel.find({}, function(err, query){
console.log("Number of objects currently in db: " + query.length);
uniqueIndex = uniqueIndex + query.length;
});
app.configure(function(){
app.set('port', process.env.PORT || 8000);
app.set('address', process.env.ADDRESS || 'localhost');
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, '/public')));
app.use(ErrorHandlr.express());
});
app.get('/', function(req, res){
res.render('index.jade');
});
app.post('/sendData', function(req, res){
res.header('Access-Control-Allow-Origin', "*");
var newKey = uniqueIndex.toString(36);
uniqueIndex++;
var newData = new VdbDataModel;
newData.key = newKey;
newData.data = req.body.JSON;
newData.save();
console.log("Data received and stored");
var returnURL = 'http://' + app.get('address');
if(app.get('port') != 80){
returnURL = returnURL + ':' + app.get('port');
}
returnURL = returnURL + '/getData?key=' + newKey;
res.json({visual_URL: returnURL});
});
app.get('/getData', function(req, res){
console.log("request received for: " + req.query.key);
VdbDataModel.find({key: req.query.key}, function(err, query){
console.log(query[0].data);
res.render('index.jade', {data: query[0].data});
});
});
server.listen(app.get('port'), function(){
console.log("Listening on port " + app.get('port'));
});