-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (55 loc) · 2.14 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
'use strict';
const spdy = require('spdy');
const fs = require('fs');
const express = require('express');
const app = express();
const options = {
key: fs.readFileSync(`${__dirname}/server.key`),
cert: fs.readFileSync(`${__dirname}/server.crt`)
};
const server = spdy.createServer(options, app);
app.get('/', (req, res) => {
const pageHTML = fs.readFileSync(`${__dirname}/static/index.html`);
const jsOptions = {
status: 200, // opcional
method: 'GET', // opcional
request: {
accept: '*/*'
},
response: {
'content-type': 'application/javascript'
}
};
let stream = res.push(`/app.js`, jsOptions);
let materializeStream = res.push('/plugins/materialize.min.js', jsOptions);
let materializeCssStream = res.push('/styles/plugins/materialize.min.css', {
request: {
accept: '*/*'
},
response: {
'content-type': 'text/css'
}
});
stream.on('error', () => {});
stream.write(`alert('HTTP/2 Server Push Habilitado :)');`); //Método write() mantém a resposta aberta
stream.end(fs.readFileSync(`${__dirname}/static/js/app.js`)); // Método end() fecha automaticamente a conexão
materializeStream.write(fs.readFileSync(`${__dirname}/node_modules/jquery/dist/jquery.min.js`));
materializeStream.end(fs.readFileSync(`${__dirname}/node_modules/materialize-css/dist/js/materialize.min.js`));
materializeCssStream.end(fs.readFileSync(`${__dirname}/node_modules/materialize-css/dist/css/materialize.min.css`));
res.set('Content-Type', 'text/html');
res.send(new Buffer(pageHTML));
});
app.use(function(req, res, next) {
const matchUrl = '/styles/fonts';
if (req.url.substring(0, matchUrl.length) === matchUrl) {
res.setHeader('content-type', 'font');
}
return next();
});
app.use(`/styles/fonts`, express.static(`${__dirname}/static/fonts`));
app.use(express.static(`${__dirname}/static`));
server.listen(process.env.PORT || 443, () => {
let host = server.address().address;
let port = server.address().port;
console.log(`Listening at: https://${host}:${port}`);
});