-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (71 loc) · 2.67 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
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
const https = require('https');
const http = require('http');
const url = require('url');
const fs = require('fs');
const { WebSocketServer, OPEN } = require('ws');
const mime = require('mime');
require('dotenv').config();
const PORT = process.env.PORT || 8080;
const WSPORT = process.env.WSPORT || 8090;
const PROTOCOL = process.env.PROTOCOL || 'http';
const LOG = process.env.LOG == 'true' || false;
var WSSERVER = null;
function serveFile(req, res) {
const splitUrl = req.url.split('.');
if (LOG)
console.log(`Handling request on: ${req.url} (${mime.getType(splitUrl[splitUrl.length - 1])})`)
const q = url.parse(req.url, true);
fs.readFile(`${__dirname}/public${q.path || '/'}`, function(err, data) {
if (err) {
fs.readFile(`${__dirname}/error/404.html`, function(err, data) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end(data);
});
} else {
res.writeHead(200, { 'Content-Type': mime.getType(splitUrl[splitUrl.length - 1]) });
res.end(data);
}
});
}
http.createServer(function (req, res) {
const q = url.parse(req.url, true);
if (PROTOCOL == 'https') {
res.writeHead(302, {'Location': `https://localhost:${PORT + 1}${q.path}`});
res.end();
}
serveFile(req, res);
}).listen(PORT);
console.log(`http listening on: https://localhost:${PORT}/`)
if (PROTOCOL == 'http') {
WSSERVER = new WebSocketServer({ port: WSPORT });
console.log(`websocket listening on: ws://localhost:${WSPORT}/`)
}
else {
https.createServer({
key: fs.readFileSync(`${__dirname}/cert/localhost.key`),
cert: fs.readFileSync(`${__dirname}/cert/localhost.crt`)
}, function (req, res) {
serveFile(req, res);
}).listen(PORT + 1);
console.log(`https listening on: https://localhost:${PORT + 1}/`)
const server = https.createServer({
cert: fs.readFileSync(`${__dirname}/cert/localhost.crt`),
key: fs.readFileSync(`${__dirname}/cert/localhost.key`)
});
WSSERVER = new WebSocketServer({ server });
server.listen(WSPORT + 1);
console.log(`websocket (secure) listening on: wss://localhost:${WSPORT + 1}/`)
}
fs.watch(`${__dirname}/public`, {
persistent: true,
recursive: true
}, (event, filename) => {
if (filename) {
console.log(`Server socket update: ${filename}:${event}`)
WSSERVER.clients.forEach(client => {
if (client.readyState === OPEN) {
client.send(`${filename}:${event}`);
}
});
}
});