-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (65 loc) · 1.43 KB
/
index.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
'use strict';
const Hapi = require('hapi');
// Parse command-line arguments
const CommandLineArgs = require('command-line-args');
const optionDefinitions = [
{name: 'port', alias: 'p', type: Number, defaultValue: 8080}
];
const options = new CommandLineArgs(optionDefinitions);
const server = new Hapi.Server();
server.connection({port: options.port});
//Use a simple in-memory map as a database for this project
const database = {};
// Expose database for tests
if (process.env.NODE_ENV === 'test') {
server.database = database;
}
const hapiPlugins = [
require('vision'),
require('inert')
];
server.register(hapiPlugins, (err) => {
if (err) throw err;
});
const routes = [
{
register: require('./routes/api/loans.js'),
options: {
database: database
}
},
{
register: require('./routes/app/loans.js')
}
];
server.register(routes, (err) => {
if (err) throw err;
//serve static files
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: 'static'
}
}
});
// load views
server.views({
engines: {
html: require('handlebars')
},
relativeTo: __dirname,
path: 'templates'
});
if (!module.parent) {
server.start(function (err) {
if (err) {
throw err;
}
console.log('Server running at: ' + server.info.uri);
});
}
});
// Export server endpoints for unit-testing
module.exports = server;