-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (52 loc) · 1.55 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
var Hapi = require('hapi'),
path = require('path'),
port = process.env.PORT || 3000,
server = new Hapi.Server(port),
routes = {
css: {
method: 'GET',
path: '/styles/{path*}',
handler: createDirectoryRoute('styles')
},
js: {
method: 'GET',
path: '/scripts/{path*}',
handler: createDirectoryRoute('scripts')
},
assets: {
method: 'GET',
path: '/assets/{path*}',
handler: createDirectoryRoute('assets')
},
templates: {
method: 'GET',
path: '/templates/{path*}',
handler: createDirectoryRoute('templates')
},
spa: {
method: 'GET',
path: '/{path*}',
handler: {
file: path.join(__dirname, '/app/index.html')
}
}
};
server.route([ routes.css, routes.js, routes.assets, routes.templates, routes.spa ]);
server.start( onServerStarted );
server.on('response', function (request) {
if(request.url.path.includes('templates')) {
console.log();
console.log(new Date().toString() + ': ' + request.method.toUpperCase() + ' - ' + request.url.path + ' - (' + request.response.statusCode + ')');
}
});
function onServerStarted() {
console.log( 'Server running on port ', port );
}
function createDirectoryRoute(directory) {
return {
directory: {
path: path.join(__dirname, '/app/', directory)
}
};
}
module.exports = server;