-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
102 lines (75 loc) · 2.24 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
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
var express = require('express');
var app = express();
var exphbs = require('express-handlebars');
var bodyParser = require('body-parser');
var _ = require('underscore');
var path = require("path");
var mongojs = require('mongojs');
// Here we find an appropriate database to connect to, defaulting to
// localhost if we don't find one.
app.use('*', function(req, res, next) {
// set CORS response header
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
var MONGOCONNECTION =
process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost:27017/bec-explorer';
var db = mongojs(MONGOCONNECTION);
function mongoError(res, err) {
if (err) console.error('mongo error ->', err);
return res.status(500).send('internal server error')
};
function findAll(collection, query, res) {
collection.find(
query,
function(err, docs) {
if (err) {
return mongoError(res, err);
};
// if nothing is found (doc === null) return empty array
res.send(docs === null ? [] : docs);
}
);
};
app.use('/', express.static('views'));
app.engine('handlebars', exphbs({
defaultLayout: 'main'
}));
app.set('view engine', 'handlebars');
// var grid = db.collection('co2grid');
// grid data
app.get('/api/test', function(req, res) {
// findAll(grid, {}, res);
console.log("Hello stranger");
});
app.get('/', function(req, res) {
// res.send('Hello World!');
res.render('index');
});
app.get('/about', function(req, res) {
// res.send('Hello World!');
res.render('about');
});
app.get('/contact', function(req, res) {
// res.send('Hello World!');
res.render('contact');
});
app.get('/howto', function(req, res) {
// res.send('Hello World!');
res.render('howto');
});
app.get('/climate-data', function(req, res) {
// res.send('Hello World!');
res.render('climate-data');
});
app.get('/api', function(req, res) {
// res.send('Hello World!');
res.render('api');
});
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});