Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- const http = require('http');
- const fs = require('fs');
- const index = fs.readFileSync('index.html');
- const poster = fs.readFileSync('poster.png');
- http.createServer(function(req, res){
- console.log(req.url)
- if (req.url == "/index.html"){
- res.writeHead(200);
- res.write(index);
- res.end();
- } else if (req.url == "/poster.png"){
- res.writeHead(200);
- res.write(poster);
- res.end();
- } else if (req.url.indexOf("video") == 1){
- const fname = req.url.slice(1);
- const type = fname.slice(fname.indexOf(".")+1);
- const length = fs.statSync(fname).size;
- res.writeHead(200, {'Accept-ranges': "None", 'Content-Type': "video/" + type, 'Content-Length': length});
- const rs = fs.createReadStream(fname);
- rs.on('error', function (err) {
- console.log("error: ", err);
- res.end(err);
- });
- rs.on('end', function(){
- console.log("read end.");
- })
- const pipe = rs.pipe(res);
- } else {
- res.writeHead(404)
- res.end();
- }
- }).listen(8080, '127.0.0.1')
Add Comment
Please, Sign In to add comment