-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
80 lines (70 loc) · 1.67 KB
/
server.js
File metadata and controls
80 lines (70 loc) · 1.67 KB
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
'use strict';
const Hapi = require('hapi');
const server = new Hapi.Server();
const hapiAsyncHandler = require('hapi-async-handler');
const vision = require('vision');
const inert = require('inert');
const hapiSwagger = require('hapi-swagger');
const AuthBearer = require('hapi-auth-bearer-token');
const BoomHelper = require('./src/common/boom-helper');
const boomHelper = new BoomHelper();
const hapiSwaggerOptions = {
info: {
title: 'Jibble Backend Test',
version: '0.0.1',
contact: {
url: 'https://github.com/sorioinc/nodejs-backend',
},
},
schemes: ['https'],
};
server.connection({
port: process.env.PORT || 3000,
routes: {
cors: true,
},
});
const plugins = [
{
register: hapiAsyncHandler,
},
{
register: vision,
},
{
register: inert,
},
{
register: hapiSwagger,
options: hapiSwaggerOptions,
},
{
register: AuthBearer,
},
];
server.register(plugins, async (err) => {
if (err) {
throw err;
}
server.auth.strategy('simple', 'bearer-access-token', {
allowQueryToken: true,
allowMultipleHeaders: false,
accessTokenName: 'access_token',
validateFunc(token, callback) {
if (token === 'af24353tdsfw') {
return callback(null, true, { token }, {});
}
return callback(null, false, { token }, {});
},
unauthorizedFunc: () => boomHelper.dispatchBoomCall({ code: 501 }),
});
server.start((error) => {
if (error) {
throw error;
}
server.realm.modifiers.route.prefix = '/api';
// eslint-disable-next-line global-require
server.route(require('./src/routes/routes'));
console.log(`server running at: ${server.info.uri}`);
});
});