-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-video.js
More file actions
36 lines (27 loc) · 852 Bytes
/
server-video.js
File metadata and controls
36 lines (27 loc) · 852 Bytes
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
const http = require('http');
const fs = require('fs');
const path = require('path');
const server = http.createServer((req, res) => {
if (req.url === '/video') {
const videoPath = path.join(__dirname, 'test.mp4');
const videoStat = fs.statSync(videoPath);
res.writeHead(200, {
'Content-Type': 'video/mp4',
'Content-Length': videoStat.size
});
const readVideoStream = fs.createReadStream(videoPath);
readVideoStream.on('data', (chunk) => {
console.count('chunk');
console.log(`
size: ${chunk.length}, bytes, readed, sended.
`)
});
readVideoStream.pipe(res);
} else {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World, route not found');
}
})
server.listen(4000, () => {
console.log('Server is running on port 4000');
});