Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to set content length of an mp3 stream in nodejs

var http = require('http'),
    fs   = require('fs'),
    filePath = '/home/risto/Downloads/oleg.mp3',
    stat = fs.statSync(filePath);

http.createServer(function(request, response) {

    response.writeHead(200, {
        'Content-Type': 'audio/mpeg',
        'Content-Length': stat.size
    });

    // We replaced all the event handlers with a simple call to util.pump()
    fs.createReadStream(filePath).pipe(response);
})
.listen(2000);
Comment

how to set content length of an mp3 stream in nodejs

var http = require('http'),
    url = require('url'),
    fs   = require('fs'),
    filePath = '/home/risto/Downloads/oleg.mp4',
    stat = fs.statSync(filePath);

http.createServer(function(request, response) {        
    const fileSize = stat.size;
    const range = request.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 readStream = fs.createReadStream(filePath, { start, end });
      const head = {
        'Content-Range': `bytes ${start}-${end}/${fileSize}`,
        'Accept-Ranges': 'bytes',
        'Content-Length': chunksize,
        'Content-Type': 'video/mp4',
      };
      response.writeHead(206, head);
      readStream.pipe(response);
    } else {
      const head = {
        'Content-Length': fileSize,
        'Content-Type': 'video/mp4',
      };
      response.writeHead(200, head);
      fs.createReadStream(filePath).pipe(response);
    }
})
.listen(2000);
Comment

PREVIOUS NEXT
Code Example
Javascript :: js on edge files 
Javascript :: node redisjson remove path 
Javascript :: why does hoisting does not work in function expressions 
Javascript :: Material App debug mode 
Javascript :: js clean nested undefined props 
Javascript :: reactjs compile subdomine 
Javascript :: react native ios pressable inside motiview 
Javascript :: flutter keep local storage even after is closed 
Javascript :: check radio button is checked jquery 
Javascript :: block enter key using jquery 
Javascript :: javascript get div x y position 
Javascript :: javascript convert string to float with 2 decimal places 
Javascript :: javascript compare two arrays of objects get same elements 
Javascript :: jquery select2 hide search box 
Javascript :: single quote error in react prettier 
Javascript :: remove multiple space to single javascript 
Javascript :: adonisjs livereload 
Javascript :: Chamando métodos de utilidade com consign( ) no nodeJs 
Javascript :: delete from array in angular 
Javascript :: last element array 
Javascript :: how to sort a populated data in mongoose 
Javascript :: google apps script moment js 
Javascript :: custom error nodejs 
Javascript :: hue api unauthorized user 
Javascript :: react-native multi line text-input 
Javascript :: javascript get index of object in array 
Javascript :: javascript find object by property in array 
Javascript :: change text using javascript 
Javascript :: angular datepicker sending value one day less than 
Javascript :: fill array with 0 javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =