-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (71 loc) · 2.05 KB
/
Copy pathindex.js
File metadata and controls
79 lines (71 loc) · 2.05 KB
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
const express = require("express");
const fs = require("fs");
const spdy = require("spdy");
const https = require("https");
// watch -n 1 ss -s
let expressApp;
const httpsOptions = {
key: fs.readFileSync("certs/privkey.pem"),
cert: fs.readFileSync("certs/cert.pem"),
ca: fs.readFileSync("certs/chain.pem"),
};
const http1Port = 443;
const http2Port = 8443;
const fileData = [];
function initExpressApp() {
expressApp = express();
expressApp.use("/http1", express.static("public"));
expressApp.use("/http2", express.static("public"));
initServerRoutes();
}
function initServerRoutes() {
expressApp.get("/", (req, res) => {
res.send("Hello World!");
});
expressApp.get("/serverpush", (req, res) => {
let htmlData = fs.readFileSync("./public/http2.html", "utf8");
for (let i = 1; i <= 384; i++) {
let imgData = fs.readFileSync(`./public/images/part-${i}.jpg`);
let stream = res.push(`/http2/images/part-${i}.jpg`, {
status: 200,
method: "GET",
request: {
accept: "*/*",
},
response: {
"content-type": "image/jpg",
"accept-ranges": "bytes",
"content-length": imgData.length,
},
});
stream.on("error", function (err) {
console.log("steam error", err);
});
stream.end(imgData);
}
res.end(htmlData);
});
}
function initHttpV1Server() {
const httpsServer = https.createServer(httpsOptions, expressApp);
//httpsServer.keepAliveTimeout = 1;
httpsServer.listen(http1Port, () => {
console.log(`HTTPS Server Listening on port: ${http1Port}`);
});
// httpsServer.on("connection", function (socket) {
// // console.log("A new connection was made by a client.");
// });
}
function initHttpV2Server() {
spdy.createServer(httpsOptions, expressApp).listen(http2Port, (error) => {
if (error) {
console.error(error);
return process.exit(1);
} else {
console.log(`HTTP2 Server Listening on port: ${http2Port}`);
}
});
}
initExpressApp();
initHttpV1Server();
initHttpV2Server();