-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-spdy.js
81 lines (69 loc) · 1.88 KB
/
index-spdy.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
const spdy = require('spdy')
const express = require('express')
const fs = require('mz/fs')
const morgan = require('morgan')
const app = express()
app.use('/_logger', require('inline-log')({limit: 100}))
app.use(morgan('dev'))
app.get('/', serveHome)
app.use(express.static('public'))
function readFiles () {
const files = [
'public/index.html',
'public/images/image1.jpg',
'public/images/image2.jpg'
]
return files.map(name => fs.readFile(name))
}
function pushFile (path, contents, options, res) {
const stream = res.push(path, options)
stream.on('error', console.error)
stream.end(contents)
}
const imageOptions = {
req: {accept: 'image/*'},
res: {'content-type': 'image/jpeg'}
}
function serveHome (req, res) {
console.log('serving home')
const homePageWithPush = files => {
if (res.push) {
console.log('browser supports HTTP/2 Push!!!',
'is SPDY?', req.isSpdy, 'spdy version', req.spdyVersion)
pushFile('/images/image1.jpg', files[1], imageOptions, res)
pushFile('/images/image2.jpg', files[2], imageOptions, res)
} else {
console.log('No HTTP/2 Push :(, is page secure?',
req.secure, 'is SPDY?', req.isSpdy)
}
// index.html is the first file
res.writeHead(200)
res.end(files[0])
}
// we could also read all files at startup
Promise.all(readFiles())
.then(homePageWithPush)
.catch(error => res.status(500).send(error.toString()))
}
const tlsOptions = {
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt'),
spdy: {
plain: false,
ssl: true
}
}
const plainOptions = {
spdy: {
plain: true,
ssl: false,
protocolos: ['h2','spdy/3.1', 'spdy/3', 'spdy/2','http/1.1', 'http/1.0'],
protocol: 'h2'
}
}
spdy.createServer(tlsOptions, app).listen(8010, err => {
if (err) {
throw new Error(err)
}
console.log('listening on port 8010')
})