forked from sclorg/nodejs-ex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverOld.js
120 lines (103 loc) · 3.81 KB
/
serverOld.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// OpenShift sample Node application
var express = require('express'),
fs = require('fs'),
app = express(),
eps = require('ejs'),
morgan = require('morgan');
Object.assign=require('object-assign')
app.engine('html', require('ejs').renderFile);
app.use(morgan('combined'))
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0',
mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL,
mongoURLLabel = "";
if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) {
var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase(),
mongoHost = process.env[mongoServiceName + '_SERVICE_HOST'],
mongoPort = process.env[mongoServiceName + '_SERVICE_PORT'],
mongoDatabase = process.env[mongoServiceName + '_DATABASE'],
mongoPassword = process.env[mongoServiceName + '_PASSWORD']
mongoUser = process.env[mongoServiceName + '_USER'];
if (mongoHost && mongoPort && mongoDatabase) {
mongoURLLabel = mongoURL = 'mongodb://';
if (mongoUser && mongoPassword) {
mongoURL += mongoUser + ':' + mongoPassword + '@';
}
// Provide UI label that excludes user id and pw
mongoURLLabel += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
}
}
var db = null,
dbDetails = new Object();
var initDb = function(callback) {
if (mongoURL == null) return;
var mongodb = require('mongodb');
if (mongodb == null) return;
mongodb.connect(mongoURL, function(err, conn) {
if (err) {
callback(err);
return;
}
db = conn;
dbDetails.databaseName = db.databaseName;
dbDetails.url = mongoURLLabel;
dbDetails.type = 'MongoDB';
console.log('Connected to MongoDB at: %s', mongoURL);
});
};
app.get('/', function (req, res) {
// try to initialize the db on every request if it's not already
// initialized.
if (!db) {
initDb(function(err){});
}
if (db) {
var data = req.query['data'];
var rand = req.query['rand'];
//data = "D2016,8,18,2,30,0;T2016,8,18,0,30,0;T2016,8,18,1,0,0;T2016,8,18,1,30,0;M2016,8,18,2,0,0;T2016,8,18,3,0,0;T2016,8,18,3,30,0;T2016,8,18,4,0,0;T2016,8,18,4,30,0;T2016,8,18,5,0,0;T2016,8,18,5,30,0;T2016,8,18,6,0,0;M2016,8,18,6,30,0;M2016,8,18,7,0,0;M2016,8,18,7,30,0;T2016,8,18,8,0,0;T2016,8,18,8,30,0;T2016,8,18,9,0,0;T2016,8,18,9,30,0";
//rand = "6ionafoa28gvh";
var col = db.collection('counts');
console.log(data);
// Create a document with request IP and current time of request
//col.insert({ip: req.ip, date: Date.now(), data: data, rand: rand});
col.count(function (err, count) {
// https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html
var d = col.find("data"); //?
console.log(d);
//if(d == null){ d = data; col.insert({data: data}); }
res.render('index.html', {
pageCountMessage: count + 8619,
dbInfo: dbDetails,
data: data
});
});
} else {
res.render('index.html', { pageCountMessage : null});
}
});
app.get('/pagecount', function (req, res) {
// try to initialize the db on every request if it's not already
// initialized.
if (!db) {
initDb(function(err){});
}
if (db) {
db.collection('counts').count(function(err, count ){
res.send('{ pageCount: ' + count + '}');
});
} else {
res.send('{ pageCount: -1 }');
}
});
// error handling
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500).send('Something bad happened!');
});
initDb(function(err){
console.log('Error connecting to Mongo. Message:\n'+err);
});
app.listen(port, ip);
console.log('Server running on http://%s:%s', ip, port);
module.exports = app ;