-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (49 loc) · 1.39 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
'use strict';
const express = require('express');
const fs = require('fs');
const path = require('path');
const mime = require('mime-types')
const app = express();
const PORT = process.env.PORT || 3003;
app.get('/', (req, res) => {
res.json('Video Streaming App');
});
app.get('/stream/:file', async (req, res) => {
const filePath = `./assets/${req.params.file}`;
let stat;
try {
stat = fs.lstatSync(filePath); // throws if path doesn't exist
} catch (e) {
return res.status(404).send({
message: '404 Not Found'
});
}
const mimeType = mime.contentType(path.extname(filePath));
const fileSize = stat.size;
const range = req.headers.range;
if (range) {
const parts = range.replace(/bytes=/, '').split('-');
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
const chunkSize = (end - start) + 1;
const file = fs.createReadStream(filePath, { start, end, highWaterMark: 256 * 1024 });
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunkSize,
'Content-Type': mimeType
};
res.writeHead(206, head);
file.pipe(res);
} else {
const head = {
'Content-Length': fileSize,
'Content-Type': mimeType
};
res.writeHead(200, head);
fs.createReadStream(filePath).pipe(res);
}
});
app.listen(PORT, () => {
console.log(`Listening of port ${PORT}`);
});