forked from shhgitdotcom/secrets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev-server.js
122 lines (116 loc) · 5.11 KB
/
dev-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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// A mini dev server.
// Uses standard Nodejs http or https server.
// Uses "connect" for various middlewares.
// Uses "socket.io" for live-reload in watch mode.
// Uses "open" to automatically open user browser.
const connect = require('connect');
const _open = require('open');
const serveStatic = require('serve-static');
const http = require('http');
const _https = require('https');
const historyApiFallback = require('connect-history-api-fallback');
const injector = require('connect-injector');
const socketIO = require('socket.io');
// Use dedicated path for the dev server socket.io.
// In order to avoid possible conflict with user app socket.io.
const socketIOPath = '/__dev_socket.io';
// Tell user browser to reload.
const socketIOSnippet = `
<script src="${socketIOPath}/socket.io.js"></script>
<script>
var socket = io({path: '${socketIOPath}'});
socket.on('reload', function() {
console.log('Reload the page');
window.location.reload();
});
</script>
`;
let io;
exports.run = function({
port = 9000,
https = false,
open = false // automatically open a browser window
} = {}) {
const app = connect()
// Inject socket.io snippet for live-reload.
// Note connect-injector is a special middleware,
// has to be applied before all other middlewares.
.use(injector(
(req, res) => {
const contentType = res.getHeader('content-type');
return contentType && (contentType.toLowerCase().indexOf('text/html') >= 0);
},
(content, req, res, callback) => {
const injected = content.toString().replace(/<\/head>/i, socketIOSnippet + '\n</head>');
callback(null, injected);
}
))
// connect-history-api-fallback is a tool to help SPA dev.
// So in dev mode, http://localhost:port/some/route will get
// the same /index.html as content, instead of 404 at /some/route.html
.use(historyApiFallback())
.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
})
.use(serveStatic('.'));
const server = https ?
_https.createServer({key: localKey, cert: localCert}, app) :
http.createServer(app);
io = socketIO(server, {path: socketIOPath});
server.listen(port);
const url = `http${https ? 's' : ''}://localhost:${port}`;
console.log(`\x1b[36m\nDev server is started at: ${url}\n\x1b[0m`);
if (open) _open(url);
};
exports.reload = function() {
io && io.emit('reload');
};
// An example of self-signed certificate.
// Expires: Tuesday, 29 January 2030
const localCert = `-----BEGIN CERTIFICATE-----
MIICpDCCAYwCCQDn7uXANbZ/wzANBgkqhkiG9w0BAQsFADAUMRIwEAYDVQQDDAls
b2NhbGhvc3QwHhcNMjAwMjAxMDA1ODQ3WhcNMzAwMTI5MDA1ODQ3WjAUMRIwEAYD
VQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC+
X/5JbY8atUrS+0nfWA/tNHHaf0W9HKwyjC74Fv2xawQOFamSUELfjHhsauVfrPqb
ONxZFAMC8GP4eRrJ8pMapN6HrIJRKMjHgImC4mOyCr7up9MqAVjvVRrrcP//XLdf
ksw70LI0In7tzu7tazKP9Ix7VBbaPBV7YKY/0b7t/m8/0elRrrnUA9xWgFadYYGy
ecgR7mvDDOIH9jzvbVr6tkhxnizlyRdpUuHcdVYGoJ97gJkm7d41Z3ICt9miKgCQ
QyhOKDsDRiXMCUH4EEOYqljJqSnMi5txXxdB11Pz64f0oIzdTPWOONLtYlzC+h7u
gK199HtIReK7J/AZpdHpAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAF5gqaldwOBj
eD23Xy0r87O4ddxIhlSYlfVFdx+9w4YXNg0rnAbBv2BEZtSIyoH9E3Uc+OKT/63b
GsICR/21Rwz40SIBSTEutWlcp7G1cpqkel3X7fE3n/t5heJjG5QjCsu8R3FOMm/r
JDwsJvooflTpHEYKERYOvmMpxWpwqA0uSRSJnFkD7w/PCNO0nwXFRZSUEyf80sa/
4kW3wgcPbsFfjKhR+dMNExHCvXojQW5XXXB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