-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
81 lines (69 loc) · 1.63 KB
/
main.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
'use strict';
const Hapi = require('hapi'),
Datastore = require('nedb'),
db = new Datastore({
filename: 'data/learned.db',
autoload: true
});
const server = new Hapi.Server();
server.connection({
port: 3000,
host: '127.0.0.1'
});
server.register(require('inert'), (err) => {
if (err) {
throw err;
}
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.file('./public/index.html');
}
});
server.route({
method: 'GET',
path: '/learned',
handler: function (request, reply) {
db.find({}, function(err, docs) {
reply(docs)
})
}
});
server.route({
method: 'POST',
path: '/learn',
config: {
handler: function (req, reply) {
db.insert(req.payload, function (err, newDoc) {
if(err) {
reply({success: false})
} else {
reply({success: true, data: newDoc})
}
});
},
payload: {
output: 'data',
parse: true
}
}
})
server.route({
method: "GET",
path: "/public/{path*}",
handler: {
directory: {
path: "./public",
listing: false,
index: false
}
}
});
});
server.start((err) => {
if (err) {
throw err;
}
console.log(`Charmaine server running at: ${server.info.uri}`);
});