Question

I have a small podcast feed that I'm hosting and after I finally got it hooked up to Downcast everything worked fine until I tried to skip forward/back. I finally realized that seeking isn't inherently part of pipe streaming. I've searched around and I cannot seem to find anything of use.

With the following code, how would I implement seeking?

app.get('/stream/:id', function(req, res) {
    if (!req.params.id) {
        res.status(404);
        res.send("Not found");
    }

    var filePath = path.join(Config.media, '**' , req.params.id);

    filePath = glob.sync(filePath)[0];

    var stat = fs.statSync(filePath);

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

    var readStream = fs.createReadStream(filePath);
    readStream.pipe(res);
});
Was it helpful?

Solution

I was actually able to do this using visionmedia/send (which handled a lot of my work with paths as well).

Here's the new code that works with seeking:

var send = require('send');

...

app.get('/stream/*', function(req, res) {
    var path = Url.parse(req.url).pathname.replace('/stream','');

    send(req, path)
        .root(Config.media)
        .on('error', function(err) {
            res.statusCode = err.status || 500;
            res.end(err.message);
        })
        .on('directory', function() {
            res.statusCode = 403;
            res.end("Forbidden");
        })
        .pipe(res);

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top