Repository navigation
Expand file tree
/
Copy pathstream_server.js
More file actions
54 lines (44 loc) · 1.43 KB
/
Copy pathstream_server.js
File metadata and controls
54 lines (44 loc) · 1.43 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
class StreamServer {
constructor(app) {
this._app = app;
this._fs = require('fs');
this._http = require('http');
this._path = require('path');
this.listen();
}
listen() {
this._http.createServer((req, res) => {
const file = this._path.resolve('./videos' + req.url);
this._fs.stat(file, (err, stats) => {
if (err) {
if (err.code === 'ENOENT') {
res.statusCode = 404;
return;
};
res.end(err);
};
if (!req.headers.range) {
res.statusCode = 416;
return;
};
const positions = req.headers.range.replace(/bytes=/, "").split("-");
const start = parseInt(positions[0], 10);
const end = positions[1] ? parseInt(positions[1], 10) : stats.size - 1;
res.writeHead(206, {
"Content-Range": "bytes " + start + "-" + end + "/" + stats.size,
"Accept-Ranges": "bytes",
"Content-Length": (end - start) + 1,
"Content-Type": "video/mp4",
});
const stream = _fs.createReadStream(file, { start, end })
.on("open", () => {
stream.pipe(res);
}).on("error", err => {
res.end(err);
});
});
}).listen(this._app.serverConfig.streamPort);
console.log('Video stream server listen on port ' + this._app.serverConfig.streamPort);
}
}
module.exports = app => new StreamServer(app);