-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
202 lines (180 loc) · 5.23 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'use strict';
const Hapi = require('hapi');
const Boom = require('boom');
const mongoose = require('mongoose');
const glob = require('glob');
const path = require('path');
const secret = require('./config');
const Hoek = require('hoek');
// const Inert = require('inert');
const server = new Hapi.Server();
// The connection object takes some
// configuration, including the port
server.connection({ port: 3000 });
// const io = require('socket.io')(server.listener);
// io.on('connection', function(socket){
// console.log(socket);
// socket.emit('Hiiiiii');
// socket.on('burp', function(){
// socket.emit('Excuse you!');
// });
// });
const dbUrl = 'mongodb://localhost:27017/user-app';
// var options = {
// reporters: [{
// reporter: require('good-console'), // Log everything to console
// events: { log: '*' }
// }, {
// reporter: require('good-file'), // Log 'debug' to debug_log.log
// events: { log: 'debug' },
// config: 'debug_log.log'
// }, {
// reporter: require('good-file'), // Log 'error' to error_log.log
// events: { log: 'error' },
// config: 'error_log.log'
// }]
// };
const options1 = {
query: {
page: {
name: 'page' // The page parameter will now be called page
},
limit: {
name: 'per_page', // The limit will now be called per_page
default: 3 // The default value will be 10
}
},
meta: {
name: 'metadata', // The meta object will be called metadata
count: {
active: true,
name: 'count'
},
pageCount: {
name: 'totalPages'
},
self: {
active: false // Will not generate the self link
},
first: {
active: false // Will not generate the first link
},
last: {
active: false // Will not generate the last link
}
},
routes: {
include: ['/api/posts'],
}
};
const options = {
ops: {
interval: 30000 // reporting interval (30 seconds)
},
reporters: {
myConsoleReporter: [{
// good-squeeze allows filtering events based on the `good` event options
// @see https://github.com/hapijs/good/blob/master/API.md#event-types
module: 'good-squeeze', // https://github.com/hapijs/good-squeeze
name: 'Squeeze',
// @example "Log everything"
args: [{ log: 'debug', error: 'error', response: '*', request: '*', ops: '*' }]
// @example "Log only request logs with a certain tag":
// args: [{ request: ['name'] }]
}, {
module: 'good-console'
}, 'stdout'],
// myFileReporter: [{
// module: 'good-squeeze',
// name: 'Squeeze',
// args: [{ log: 'debug', error: 'error', response: '*', request: '*', ops: '*' }]
// }, {
// module: 'good-squeeze',
// name: 'SafeJson'
// }, {
// module: 'good-file',
// args: ['./awesome_log']
// }],
myHTTPReporter: [{
module: 'good-squeeze',
name: 'Squeeze',
args: [{ log: 'debug', error: 'error', response: '*', request: '*', ops: '*' }]
}, {
module: 'good-http',
args: ['http://localhost:3000/api/log', {
wreck: {
headers: { 'x-api-key': 12345 }
}
}]
}]
}
};
// var validate = function (decoded, request, callback) {
// console.log(decoded.username, 'pritesh');
// // do your checks to see if the person is valid
// // if (!people[decoded.id]) {
// // return callback(null, false);
// // }
// // else {
// // return callback(null, true);
// // }
// };
server.register([
require('hapi-auth-jwt'),
require('vision'),
{
register: require('good'),
options: options
},
{
register: require('hapi-pagination'),
options: options1
},
{
register:require('./route/plugins/chat')
}
], (err) => {
//Configuration for rendering templates
server.views({
engines: {
html: require('handlebars')
},
relativeTo: __dirname,
path: 'route/templates'
});
// We're giving the strategy both a name
// and scheme of 'jwt'
server.auth.strategy('jwt', 'jwt', {
key: secret,
// validateFunc: validate,
verifyOptions: {
ignoreExpiration: true,
algorithms: ['HS256']
}
});
// Look through the routes in
// all the subdirectories of route
// and create a new route for each
glob.sync('route/**/routes/**/*.js', {
root: __dirname
}).forEach(file => {
const route = require(path.join(__dirname, file));
// console.log(route);
// reply(route);
server.route(route);
});
// Start the server
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
// Once started, connect to Mongo through Mongoose
mongoose.Promise = global.Promise;
mongoose.connect(dbUrl, {}, (err) => {
if (err) {
throw err;
}
});
});
});