-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.js
56 lines (49 loc) · 1.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
import path from 'path';
import Hapi from 'hapi';
import Inert from 'inert';
import { hapi } from '@risingstack/graffiti';
import { getSchema } from '@risingstack/graffiti-mongoose';
import mongoose from 'mongoose';
import mongooseSchema from './data/schema';
import Filter from 'bad-words';
const PORT = process.env.PORT || 8080;
const MONGO_URI = process.env.MONGO_URI || process.env.MONGOLAB_URI || 'mongodb://localhost/graphql';
mongoose.connect(MONGO_URI);
const server = new Hapi.Server();
server.connection({ port: PORT });
const filter = new Filter();
const hooks = {
mutation: {
pre: (next, todo, ...rest) => {
if (todo.text) {
todo.text = filter.clean(todo.text);
}
next(todo, ...rest);
}
}
};
server.register([Inert, {
register: hapi,
options: {
schema: getSchema(mongooseSchema, { hooks })
}
}], (err) => {
if (err) {
throw new Error('Failed to load a plugin: ' + err);
}
});
// serve static files
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: path.join(__dirname, 'public'),
redirectToSlash: true,
index: true
}
}
});
server.start(() => {
console.log('Server running at:', server.info.uri); // eslint-disable-line
});