node.js - 如何将搜索与 Node Express 音频流结合起来

标签 node.js express streaming audio-streaming seek

我正在托管一个小型播客提要,在我最终将其连接到 Downcast 后,一切正常,直到我尝试向前/向后跳。我终于意识到寻找本质上并不是管道流的一部分。我四处搜寻,似乎找不到任何有用的东西。

使用以下代码,我将如何实现查找?

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);
});

最佳答案

我实际上可以使用visionmedia/send来做到这一点(它也处理了我的很多路径工作)。

这是用于查找的新代码:

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);

});

关于node.js - 如何将搜索与 Node Express 音频流结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22284756/

相关文章:

c# - 您如何通过单击 asp.net 中的图像按钮流式传输 zip 文件?

node.js - 如何将 EPSG 坐标转换为纬度/经度?

node.js - angular-jwt 如何在没有 secret 的情况下解码我的 JWT?

node.js - React webapp在PROD环境中不断加载本地主机资源

jquery - 如何在nodeJs中使用nodemon调试应用程序

php - 创建像 twitch 或 ustream 这样的流媒体网站?

windows-phone-7 - Windows Phone 7 上的 MediaElement 和 AAC 流支持

javascript - 登录后在哪里以及如何更改 session 用户对象?

node.js - 在 pm2 中从 fork 切换到集群模式

node.js - 多个 sailsjs 应用程序在同一端口上运行