This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
100 lines (86 loc) · 3.01 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
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
// System imports
const path = require('path')
// External modules
const debug = require('debug')
const nconf = require('nconf')
const Koa = require('koa')
// Logging
const log = debug('robototes-website-web:server')
const logHTTP = debug('http')
// Load configuration
nconf.env()
.required([
'PORT',
'IP',
'DOMAIN',
'G_TRACKING_ID',
'DATABASE_HOST',
'DATABASE_PORT'
])
log('Loaded configuration')
// Load middleware and middleware configuration
let middlewares = require('koa-load-middlewares')()
middlewares.cors = require('kcors')
middlewares.config = require('./configs/middleware')
const Pug = middlewares.pug
// Local code
const router = require('./routes/')
// Create a new app
const app = new Koa()
// Initializes and attaches Pug
let pug = new Pug({
viewPath: path.resolve(__dirname, 'views', 'pages'),
basedir: path.resolve(__dirname, 'views', 'partials'),
debug: nconf.get('DEBUG') != null,
pretty: false,
locals: {
socialMedia: require('./configs/social.js'),
nconf
}
})
pug.use(app)
log('Initialized Pug')
// Middleware
app.use(async (ctx, next) => {
logHTTP(`<-- ${ctx.path}`)
try {
await next()
// Get the status of any responses or assume the request wasn't handled
ctx.status = ctx.status || 404
// Throw any error codes, or just report and continue
if (ctx.status >= 400) ctx.throw(ctx.status)
else logHTTP(`\t--> ${ctx.status} OK`)
} catch (err) {
err.status = err.status || 500 // Make sure we have a status code
// Render the error page
ctx.render('error', {
error: err
})
ctx.status = err.status // Correct the response back to an error response (since ctx.render changes it to 200)
// Tell Koa that we've handled an error
ctx.app.emit('err', err, ctx)
// Log the error and our response
logHTTP(err)
logHTTP(`\t--> ${ctx.status} NOT OK: ${err.message}`)
}
})
.use(middlewares.bodyparser())
.use(middlewares.helmet.contentSecurityPolicy(middlewares.config.helmet.contentSecurityPolicy)) // CSP
.use(middlewares.helmet.expectCt(middlewares.config.helmet.expectCt)) // Expect-CT
.use(middlewares.helmet.referrerPolicy(middlewares.config.helmet.referrerPolicy)) // Sets Referrer-Policy header
.use(middlewares.helmet.xssFilter(middlewares.config.helmet.xssFilter))
.use(middlewares.helmet.frameguard(middlewares.config.helmet.frameguard)) // Prevents framing
.use(middlewares.helmet.hidePoweredBy()) // Removes X-Powered-By header
.use(middlewares.helmet.ieNoOpen())
.use(middlewares.helmet.noSniff()) // Prevents MIME type sniffing
.use(middlewares.cors(middlewares.config.cors)) // Enables CORS
.use(middlewares.cacheControl(middlewares.config.cacheControl))
.use(middlewares.favicon(middlewares.config.favicon))
.use(middlewares.compress()) // Compresses responses
.use(router.routes())
.use(router.allowedMethods())
log('Configured routing')
// Start the server
module.exports = app.listen(nconf.get('PORT'), nconf.get('IP'), () => {
log(`Server listening on port ${nconf.get('PORT')}`)
})