-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·135 lines (111 loc) · 3.83 KB
/
Copy pathserver.js
File metadata and controls
executable file
·135 lines (111 loc) · 3.83 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
This runs a https server, which will serve the files in this scripts directory,
and also attaches socket.io to it, which allows the YoutubeStreamerServer and YoutubeDownloadServer to work.
*/
var https = require('https'),
url = require("url"),
path = require("path"),
fs = require("fs"),
mime = require("mime");
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
//set your desired server port here
var port = 8443;
//set the https server ssl certificates here
var httpsOptions = {
key: fs.readFileSync('ssl/ssl_certificate.key'),
cert: fs.readFileSync('ssl/ssl_certificate.crt'),
ca: fs.readFileSync('ssl/ssl_certificate_ca.pem')
};
//create https server
//it will serve files in the current directory,
//or print a list for 'index.html' request
var server = https.createServer(httpsOptions, function(request, response) {
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), uri);
fs.stat(filename, function(error, data) {
if (error) {
response.writeHead(404, { "Content-Type": "text/plain" });
response.end("Error: " + uri + " not found!");
return;
}
if (data.isDirectory()) {
if (!filename.endsWith("/")) {
response.writeHead(302, { "Location": (uri + "/") });
response.end();
return;
}
fs.readFile(filename + "index.html", "binary", function(error, data) {
if (error) {
switch (error.code) {
//index.html doesn't exist, so print file list..
case 'ENOENT':
fs.readdir(filename, function(error, data) {
var file, fileList = "", dirList = "";
for (item in data) {
file = fs.statSync(filename + data[item]);
if (file.isFile()) {
fileList += "<li><a href='" + data[item] + "'>" + data[item] + "</a></li>";
} else if (file.isDirectory()) {
dirList += "<li><a href='" + data[item] + "/'>" + data[item] + "/</a></li>";
}
}
response.writeHead(200, { "Content-Type": "text/html" });
response.end("<ul>" + dirList + fileList + "</ul>");
});
break;
//unknown error
default:
response.writeHead(500, { "Content-Type": "text/plain" });
response.end("Unknown error occured: " + error.type + " -> " + error.message);
break;
}
return;
}
//index.html exists, so serve it
response.writeHead(200, { "Content-Type": "text/html" });
response.write(data, "binary");
response.end();
});
return;
}
if (data.isFile()) {
fs.readFile(filename, "binary", function(error, data) {
if (error) {
console.log("Error: " + error);
switch (error.code) {
//file doesn't exist, so 404
case 'ENOENT':
response.writeHead(404, { "Content-Type": "text/plain" });
response.end("Error: " + filename + " doesn't exist!");
break;
//unknown error
default:
response.writeHead(500, { "Content-Type": "text/plain" });
response.end("Unknown error occured: " + error.type + " -> " + error.message);
break;
}
return;
}
//file exists, so serve it
response.writeHead(200, { "Content-Type": mime.lookup(filename) });
//response.writeHead(200, {"Content-Type": "text/plain"});
response.write(data, "binary");
response.end();
});
return;
}
});
});
server.on('listening', function() {
//attach the socket.io to https server
var io = require('socket.io')();
io.serveClient(true);
//start the /stream and /download endpoints
var youtubeStreamerServer = new (require('./YoutubeStreamerServer.js').YoutubeStreamerServer)(io);
var youtubeDownloaderServer = new (require('./YoutubeDownloaderServer.js').YoutubeDownloaderServer)(io);
//var youtubeSearchServer = new (require('./YoutubeSearchServer.js').YoutubeSearchServer)(io);
io.attach(server);
});
server.listen(port);