-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (57 loc) · 1.6 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
var express = require('express');
var app = express();
var db = require('./modules/db');
var port = process.env.PORT || 3000;
app.post('/properties', function(req, res) {
db.select(['id', 'value'], 'property', {}).then(function(props) {
res.json(props);
});
});
app.post('/countries', function(req, res) {
db.select(['id', 'name'], 'country', {}).then(function(countries) {
res.json(countries);
})
});
app.post('/property/:id', function(req, res) {
db.select(['country_id', 'value'], 'country_property', {'property_id':req.params.id}).then(function(props) {
var propDict = {};
for (var i = 0; i < props.length; i++) {
propDict[props[i].country_id] = props[i].value;
}
res.json(propDict);
});
})
app.use('/', express.static(__dirname + '/public'));
app.listen(port, function() {
console.log('Listening on port ' + port);
});
//error handling
process.stdin.resume();
function exitHandler(callback) {
console.log("Node.js server shutting down gracefully.")
db.done()
if (callback == null) {
process.kill(process.pid)
} else {
callback()
}
}
process.on('exit', function() {
console.log('Exiting...')
exitHandler(null);
})
process.on('SIGINT', function() {
console.log("SIGINT error.")
exitHandler(null)
})
process.on('uncaughtException', function(err) {
console.log('Uncaught Exception!: '+err.stack)
exitHandler(null)
})
//Catch when gulp is restarting everything
process.once('SIGUSR2', function() {
console.log("SIGUSR2 restart.")
exitHandler(function() {
process.kill(process.pid, 'SIGUSR2')
});
})