3
我試圖使用流來的NodeJS的HTML5視頻播放器的視頻和表達。很多人從我能找到的東西之前就已經做過了,但是很少有人使用過快遞(在我發現的內容中)。視頻流與和的NodeJS快速
大多數人說這樣做:
var express = require('express');
var request = require('request');
var stylus = require('stylus');
var fs = require('fs');
var path = require('path');
var url = require('url');
var http = require('http');
var app = express();
app.use(stylus.middleware({
// Source directory
src: __dirname + '/assets/stylesheets',
// Destination directory
dest: __dirname + '/public',
// Compile function
compile: function(str, path) {
return stylus(str)
.set('filename', path)
.set('compress', true);
}
}));
app.enable('trust proxy');
// Routing to the user
app.use(express.static(__dirname + "/public"));
var server = require('http').createServer(app);
// var io = require('socket.io')(server);
var port = process.env.PORT || 3000;
server.listen(port, function() {
console.log("Server is listening on port " + port.toString());
});
app.post('/playVideo', function(req, res) {
var video = req.query.video;
var file = path.resolve(__dirname,video);
var range = req.headers.range;
var positions = range.replace(/bytes=/, "").split("-");
var start = parseInt(positions[0], 10);
fs.stat(file, function(err, stats) {
var total = stats.size;
var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
var chunksize = (end - start) + 1;
res.writeHead(206, {
"Content-Range": "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type": "video/avi"
});
var stream = fs.createReadStream(file, { start: start, end: end })
.on("open", function() {
stream.pipe(res);
}).on("error", function(err) {
res.end(err);
});
});
});
不幸的是,當我試圖做到這一點,我無法獲得req.headers
對象。它沒有定義。我認爲這是因爲我使用Express,儘管我必須假設它仍然可以使用Express。
我怎樣才能使用快車的頭?
謝謝!
你確定嗎?你能給完整的代碼 – BlackMamba
@BlackMamba,我通過添加發布請求和服務器設置進行更新。 –