-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (34 loc) · 1.09 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
'use strict'
const { readFileSync } = require('fs')
const { join } = require('path')
const onHeaders = require('on-headers')
const validate = require('./lib/validate')
const onHeadersListener = require('./lib/on-headers-listener')
const socketIoInit = require('./lib/socket-io-init')
module.exports = main
function main (config) {
config = validate(config)
const staticPath = join(__dirname, 'static')
const renderedHtml =
readFileSync(join(staticPath, 'index.html'), { encoding: 'utf8' })
.replace(/{{title}}/g, config.title)
.replace(/{{script}}/g, readFileSync(join(staticPath, 'app.js')))
.replace(/{{style}}/g, readFileSync(join(staticPath, 'style.css')))
return {
config,
get path () {
return config.path
},
middleware ({ rawReq, rawRes }, next) {
const startTime = process.hrtime()
socketIoInit(rawReq.socket.server, config.spans)
onHeaders(rawRes, () => {
onHeadersListener(rawRes.statusCode, startTime, config.spans)
})
return next()
},
page ({ rawRes }) {
rawRes.end(renderedHtml)
}
}
}