71 lines
2.1 KiB
JavaScript
71 lines
2.1 KiB
JavaScript
const express = require('express')
|
|
const fs = require('fs')
|
|
|
|
const app = express()
|
|
const cors = require("cors");
|
|
|
|
const videoFileMap={
|
|
'cdn':'videos/v1.mp4',
|
|
'generate-pass':'videos/v2.mp4',
|
|
'get-post':'videos/v3.mp4',
|
|
}
|
|
|
|
var corsOptions = {
|
|
origin: ['http://localhost:3000',
|
|
'http://localhost:3040',
|
|
'http://10.0.0.248:3000',
|
|
'https://10.0.0.248:3000',
|
|
'http://localhost:9082/',
|
|
'http://10.204.5.100:9082',
|
|
'http://localhost:9083/',
|
|
'https://dev-users.wrenchboard.com/',
|
|
'https://dev-users.wrenchboard.com:3000/',
|
|
'https://users.wrenchboard.com/',
|
|
'https://www.wrenchboard.com/',
|
|
'https://www.wrenchboard.ng/',
|
|
'https://dev-www.wrenchboard.com/',
|
|
'http://76.209.103.227:30040'],
|
|
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
|
|
}
|
|
|
|
|
|
app.get('/videos/:filename',cors(corsOptions), (req, res)=>{
|
|
const fileName = req.params.filename;
|
|
const filePath = videoFileMap[fileName]
|
|
if(!filePath){
|
|
return res.status(404).send('File not found')
|
|
}
|
|
|
|
const stat = fs.statSync(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});
|
|
const head = {
|
|
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
|
|
'Accept-Ranges': 'bytes',
|
|
'Content-Length': chunksize,
|
|
'Content-Type': 'video/mp4'
|
|
};
|
|
res.writeHead(206, head);
|
|
file.pipe(res);
|
|
}
|
|
else{
|
|
const head = {
|
|
'Content-Length': fileSize,
|
|
'Content-Type': 'video/mp4'
|
|
};
|
|
res.writeHead(200, head);
|
|
fs.createReadStream(filePath).pipe(res)
|
|
}
|
|
})
|
|
|
|
app.listen(3036, ()=>{
|
|
console.log('server is listening on post 3036')
|
|
}) |