Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (42 loc) · 1.38 KB
/
Copy pathapp.js
File metadata and controls
48 lines (42 loc) · 1.38 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
const express = require("express");
const path = require("path");
const fs = require("fs");
const app = express();
// app.use(express.static(path.join(__dirname, "../build")));
app.get("/mock", (req, res) => {
res.send("hello world");
});
app.get("/video", (req, res) => {
const filePath = path.resolve(__dirname, "./videos/myvideo.mkv");
const stat = fs.statSync(filePath);
const fileSize = stat.size;
const range = req.headers.range;
const contentType = "video/x-matroska";
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 });
const head = {
"Content-Range": `bytes ${start}-${end}/${fileSize}`,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type": contentType,
};
res.writeHead(206, head);
file.pipe(res);
} else {
const head = {
"Content-Length": fileSize,
"Content-Type": contentType,
};
res.writeHead(200, head);
fs.createReadStream(filePath).pipe(res);
}
});
// app.get("/*", (req, res) => {
// console.log("gotten");
// res.sendFile(path.join(__dirname, "../build", "index.html"));
// });
app.listen(5000, () => console.log("Server listening on port 5000!"));