-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (43 loc) · 1.21 KB
/
app.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
'use strict';
const Hapi = require('hapi');
const MongoClient = require('mongodb').MongoClient;
const Promise = require("bluebird");
const assert = require('assert');
const url = 'mongodb://readonly:[email protected]:43348/dev-challenge';
// server config
const server = new Hapi.Server();
server.connection({
port: process.env.PORT || 5000
});
// routes
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.file('/index.html');
}
});
server.route({
method: 'GET',
path:'/hello',
handler: function (request, reply) {
return reply('hello world!');
}
});
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected to database.");
process.on('exit', exitHandler.bind(null,{cleanup:true, db}));
process.on('SIGINT', exitHandler.bind(null, {exit:true, db}));
process.on('uncaughtException', exitHandler.bind(null, {exit:true, db}));
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
});
function exitHandler(options, err) {
console.log("Cleanup...");
options.db.close();
}