-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (57 loc) · 1.47 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
import Hapi from 'hapi'
import React from 'react'
import { readFileSync } from 'fs'
import App from './lib'
const template = readFileSync('./dist/index.html').toString()
const defaultConfig = {
cache: {
expiresIn: 30 * 24 * 3600000,
privacy: 'public'
}
}
var server = new Hapi.Server()
server.connection({
host: '0.0.0.0',
port: 3000
})
if (process.env.NODE_ENV === 'production') {
console.log('Enabling redirect.')
server.ext('onRequest', (request, reply) => {
if (request.headers.host !== 'wallpul.se') {
return reply.redirect('https://wallpul.se' + request.path)
}
reply.continue()
})
}
server.ext('onPreResponse', (request, reply) => {
if (request.response.isBoom) {
request.response.output.payload = request.response.message
}
reply.continue()
})
server.on('response', (request) => {
const ip = request.headers['x-forwarded-for'] || request.info.remoteAddress
console.log(`${ip} - ${request.response.statusCode} ${request.method.toUpperCase()} ${request.url.path}`)
})
server.route({
method: 'GET',
path: '/',
handler: (request, reply) => {
const rendered = React.renderToString(<App />)
reply(template.replace('<div id="app"></div>', `<div id="app">${rendered}</div>`))
},
config: defaultConfig
})
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: 'dist'
}
},
config: defaultConfig
})
server.start(() => {
console.log('Server running at:', server.info.uri)
})