-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
67 lines (57 loc) · 1.41 KB
/
index.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
var Hapi = require('hapi')
var PouchDB = require('pouchdb')
var memdown = require('memdown')
var inert = require('inert')
var path = require('path')
var EventEmitter = require('events').EventEmitter
var hapiStore = require('../server')
var browserify = require('browserify')([], {
standalone: 'hoodie'
})
browserify.require(path.join(__dirname, 'client.js'))
var bundleEE = new EventEmitter()
var STOREJS = null
bundleEE.on('done', function (error, buffer) {
if (error) throw error
STOREJS = buffer
})
browserify.bundle(bundleEE.emit.bind(bundleEE, 'done'))
var server = new Hapi.Server({})
server.connection({
port: 4663
})
server.route({
method: 'GET',
path: '/hoodie/client.js',
handler: function (request, reply) {
if (!STOREJS) {
return bundleEE.once('done', function (error, buffer) {
if (!error) reply(buffer).type('application/javascript')
})
}
reply(STOREJS).type('application/javascript')
}
})
server.register(inert, function (err) {
if (err) throw err
})
server.register({
register: hapiStore,
options: {
PouchDB: PouchDB.defaults({
db: memdown
})
}
}, function (error) {
if (error) throw error
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.file(path.join(__dirname, '..', 'public', 'index.html'))
}
})
})
server.start(function () {
console.log('Server running at %s', server.info.uri)
})