-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
119 lines (91 loc) · 2.35 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const Koa = require('koa')
const body = require('koa-body')
const serve = require('koa-static')
const helmet = require('koa-helmet')
const noTrailingSlash = require('koa-no-trailing-slash')
const app = require('./lib/app')
const purge = require('./lib/purge')
const router = require('./lib/router')
const api = require('./lib/middleware/api')
const cache = require('./lib/middleware/cache')
const assets = require('./lib/middleware/assets')
const render = require('./lib/middleware/render')
const prismic = require('./lib/middleware/prismic')
const redirects = require('./lib/middleware/redirects')
const imageproxy = require('./lib/middleware/cloudinary-proxy')
const server = new Koa()
/**
* Compile and serve assets on demand during development
*/
if (process.env.NODE_ENV === 'development') {
// Serve live client resources
server.use(require('./lib/middleware/watchify'))
server.use(require('./lib/middleware/postcss'))
// Serve components assets from disk
server.use(serve('lib'))
}
/**
* Take extra care to clean up em' headers in production
*/
if (process.env.NODE_ENV !== 'development') {
server.use(helmet())
}
/**
* Prevent indexing everything but production
*/
server.use(require('./lib/middleware/robots'))
/**
* Proxy cloudinary on-demand-transform API
*/
server.use(imageproxy)
/**
* Capture special routes before any other middleware
*/
server.use(api)
server.use(redirects)
/**
* Remove trailing slashes before continuing
*/
server.use(noTrailingSlash())
/**
* Serve static files
*/
server.use(assets)
if (process.env.NODE_ENV !== 'development') {
server.use(serve('public', { maxage: 1000 * 60 * 60 * 24 * 365 }))
}
/**
* Set up request cache mechanism
*/
server.use(cache)
/**
* Parse request body
*/
server.use(body())
/**
* Handle rendering response
*/
server.use(render(app))
/**
* Hook up the Prismic api
*/
server.use(prismic)
/**
* Hook up em' routes
*/
server.use(router)
/**
* Lift off
*/
if (process.env.HEROKU && process.env.NODE_ENV !== 'development') {
purge(['/service-worker.js'], (err) => {
if (err) return console.error(err)
server.listen(process.env.PORT, () => {
console.info(`🚀 Server listening at localhost:${process.env.PORT}`)
})
})
} else {
server.listen(process.env.PORT, () => {
console.info(`🚀 Server listening at localhost:${process.env.PORT}`)
})
}