-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (44 loc) · 1.27 KB
/
server.js
File metadata and controls
51 lines (44 loc) · 1.27 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
'use strict';
require('dotenv').config();
const Hapi = require('hapi');
const Boom = require('boom');
const mongoose = require('mongoose');
const glob = require('glob');
const path = require('path');
const server = new Hapi.Server();
// The connection object takes some
// configuration, including the port
server.connection({ port: process.env.PORT || 3001, routes: { cors: true } });
server.register(require('hapi-auth-jwt'), err => {
// We are giving the strategy a name of 'jwt'
server.auth.strategy('jwt', 'jwt', 'required', {
key: process.env.SECRET_KEY,
verifyOptions: { algorithms: ['HS256'] }
});
// Look through the routes in
// all the subdirectories of API
// and create a new route for each
glob.sync('api/**/routes/*.js', { root: __dirname }).forEach(file => {
const route = require(path.join(__dirname, file));
if (route.method && route.path) {
server.route(route);
}
});
});
// Start the server
server.start(err => {
if (err) {
throw err;
}
// Once started, connect to Mongo through Mongoose
mongoose.connect(
process.env.MLAB_URL,
{ useNewUrlParser: true, useUnifiedTopology: true },
err => {
if (err) {
throw err;
}
}
);
console.info(`Server started at ${server.info.uri}`);
});