Created
August 19, 2019 06:38
Star
You must be signed in to star a gist
A NodeJS sample for uploading videos and then streaming the video with range feature. Multiparty is a NPM module for uploading file in Node.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createServer } = require('http'); | |
const { promisify } = require('util'); | |
const multiparty = require('multiparty'); | |
const { | |
stat, | |
createReadStream, | |
createWriteStream | |
} = require('fs'); | |
const file = __dirname+'/powder-day.mp4'; | |
const fileInfo = promisify(stat); // stat is a filesystem feature that would let you get a file's information | |
// function for serving the video | |
const respondWithVideo = async (req, res) => { | |
const { size } = await fileInfo(file); // get the size info for the video file | |
const range = req.headers.range; // this is useful for skimming, if the user clicks some part of the video | |
// if range is present, range is vital coz some browsers won't work without it | |
if (range) { | |
let [start, end] = range.replace(/bytes=/, '').split('-'); | |
start = parseInt(start, 10); // start of the video | |
end = end ? parseInt(end, 10) : size - 1; // if there's an end, set it, otherwise size (- 1) means whole length of the video | |
// 206 means a part only | |
res.writeHead(206, { | |
'Content-Range': `bytes ${start}-${end}/${size}`, // range means start minus end divided by size | |
'Accept-Ranges': 'bytes', | |
'Content-Length': (end-start) + 1, | |
'Content-Type': 'video/mp4' | |
}); | |
createReadStream(file, { start, end }).pipe(res); | |
} else { | |
// if no range is present | |
res.writeHead(200, { | |
'Content-length': size, | |
'Content-Type': 'video/mp4' | |
}); | |
createReadStream(file).pipe(res); | |
} | |
}; | |
createServer((req, res) => { | |
if (req.method === 'POST') { | |
// if a file is being uploaded | |
// req.pipe(res); // show the file to the browser | |
// req.pipe(process.stdout); // show the file to the console | |
// req.pipe(createWriteStream( __dirname+'/upload.file')) // and upload as a file | |
// using multiparty library | |
let form = new multiparty.Form(); // instance of multiparty | |
form.on('part', (part) => { | |
part | |
.pipe(createWriteStream(`./${part.filename}`)) // upload the file with WriteStream | |
.on('close', () => { | |
res.writeHead(200, { 'Content-Type': 'text/html'}); // if done uploading respond with an html file | |
res.end(`<h1>File uploaded; ${part.filename}</h1>`) | |
}); | |
}); | |
form.parse(req); | |
} else if (req.url === '/video') { | |
// if video is requested, just render the video | |
respondWithVideo(req, res); | |
} else { | |
// if nothing, show a form for uploading | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
res.end(` | |
<form enctype="multipart/form-data" method="POST" action="/"> | |
<input type="file" name="upload-file"> | |
<button>Upload File</button> | |
</form> | |
`); | |
} | |
}).listen(3000, () => console.log('server running on port: 3000')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment