-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
83 lines (66 loc) · 2.4 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
'use strict';
const Hapi = require('@hapi/hapi');
const Inert = require('@hapi/inert');
const Vision = require('@hapi/vision');
const HapiSwagger = require('hapi-swagger');
const packageJson = require('./package.json');
// Routes definitions array, local module
const routes = require('./lib/routes.js');
// Load server configuration data
const serverConfig = require('./configs/server.json');
// Create a Hapi server instance
// If you plan to deploy your hapi application to a PaaS provider,
// you must listen on host 0.0.0.0 rather than localhost or 127.0.0.1
const server = new Hapi.Server({
host: serverConfig.host,
port: serverConfig.port,
router: {
isCaseSensitive: false,
stripTrailingSlash: true // removes trailing slashes on incoming paths
}
});
// Serve all routes defined in the routes array
// server.route() takes an array of route objects
server.route(routes);
// Register plugins and start the server
const init = async function() {
// Register invert plugin to serve CSS and JS static files
await server.register(Inert);
// Register vision plugin to render view templates
await server.register(Vision);
// HapiSwagger settings for API documentation
const swaggerOptions = {
info: {
title: 'DeepPhe-Viz API Documentation',
version: packageJson.version, // Use Viz version as API version, can be different though
},
};
// Register HapiSwagger
await server.register(
{
plugin: HapiSwagger,
options: swaggerOptions
});
// View templates rendering
server.views({
// Using handlebars as template engine responsible for
// rendering templates with an extension of .html
engines: {
html: require('handlebars')
},
isCached: false, // Tell Hapi not to cache the view files, no need to restart app
// Tell the server that our templates are located in the templates directory within the current path
relativeTo: __dirname,
path: './client/templates',
layoutPath: './client/templates/layout',
helpersPath: './client/templates/helpers'
});
// Start the server
await server.start();
console.log(`DeepPhe-Viz HTTP Server is running at: ${server.info.uri}`);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();