我正在尝试创建一个小型 nodejs 服务器,它将接受将流式传输音频的请求。我目前正在使用 0.10.5 api。我必须承认我是 nodejs 的新手。我不确定如何编辑此代码以成功流式传输音频。
var http = environment.http;
//Steaming Audio
var path = environment.path,
util = environment.util,
url = environment.url;
http.createServer(function (request, response) {
var queryData = url.parse(request.url, true).query;
window.console.log(queryData);
if (!queryData.hasOwnProperty('file')) {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.end();
window.console.log('In here');
} else {
window.console.log('In here2');
var filePath = queryData.file;
var extension = self.getExtension(filePath);
var acceptedExtensions = ['mp3', 'wav', 'aiff'];
if ($.inArray(extension, acceptedExtensions) == -1) {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.end();
}
var stat = environment.fs.statSync(filePath);
response.writeHead(200, {
'Content-Type': 'audio/mpeg',
'Content-Length': stat.size
});
//window.console.log(filePath);
var readStream = environment.fs.createReadStream(filePath);
//var readable = new require('stream').Readable();
readStream.pipe(response);
}
}).listen(10000);
当我提出请求时
http://localhost:10000/?file=/Users/acasanova/Music/Cass%20Beats/45th.mp3
,控制台显示“In here2”,然后显示“In here”。我试图弄清楚为什么它似乎两次满足请求以及为什么音频没有被流式传输。