Files
WrenchBoardMediaMicro/index.js
T
CHIEFSOFT\ameye fbf536b6ac added files
2024-03-25 06:40:01 -04:00

75 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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',
'tt1':'/opt/wrenchboard/TEST/COMMON/MEDIA/What Is Single Sign-on (SSO) How It Works-(1080p25).mp4',
'tt2':'/opt/wrenchboard/TEST/COMMON/MEDIA/What is RabbitMQ-(1080p30).mp4',
'tt3':'/opt/wrenchboard/TEST/COMMON/MEDIA/What is CORS-(1080p50).mp4',
'tt4':'/opt/wrenchboard/TEST/COMMON/MEDIA/SAML vs OAuth vs OIDC (explained simply!)-(1080p25).mp4',
'tt5':'/opt/wrenchboard/TEST/COMMON/MEDIA/Running RabbitMQ Locally with Docker-(1080p60).mp4',
'tt6':'/opt/wrenchboard/TEST/COMMON/MEDIA/React Proxy Easiest Fix to CORS Errors-(2160p24).mp4',
'tt7':'/opt/wrenchboard/TEST/COMMON/MEDIA/RabbitMQ Tutorial - Publisher and Consumer program with example in nodeJS-(686p30).mp4',
'tt8':'/opt/wrenchboard/TEST/COMMON/MEDIA/RabbitMQ - RPC with NodeJs (request-reply pattern)-(1080p60).mp4',
'tt9':'/opt/wrenchboard/TEST/COMMON/MEDIA/How to Install RabbitMQ Locally with Docker-(1080p60).mp4',
}
console.log("Finding File");
var corsOptions = {
origin: 'http://127.0.0.1:3000/',
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
app.use(cors(corsOptions));
// ,cors(corsOptions)
app.get('/videos/:filename', (req, res)=>{
const fileName = req.params.filename;
const filePath = videoFileMap[fileName]
if(!filePath){
console.log("Finding File Not Found ", filePath);
return res.status(404).send('File not found')
}
console.log("Finding File Found ", filePath);
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')
})