【发布时间】:2017-03-17 00:04:34
【问题描述】:
我在从我的 nodejs 服务器提供的 Safari 上加载 mp4 视频文件时遇到问题。 Chrome 上不存在该问题。
为了说明这个问题,我有一个简单的 html 网页,它读取 w3schools 提供的 mp4 文件 mov_bbb.mp4。然后我下载相同的文件并从我的 nodejs 服务器上提供它。
vid1(来自 w3schools)在 Chrome 和 Safari 上都可以正常加载。
vid2(由我的 nodejs 服务器提供)在 Chrome 上加载正常,但在 Safari 上加载不正常。
这是我在 Chrome 中看到的屏幕截图:
这是我在 Safari 中看到的屏幕截图:
这是我的html:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<video id="vid1" controls preload="auto" src="https://www.w3schools.com/html/mov_bbb.mp4"></video>
<video id="vid2" controls preload="auto" src="http://localhost:8125/mov_bbb.mp4"></video>
</body>
</html>
这是我的 nodejs 服务器:
var http = require('http');
var fs = require('fs');
var path = require('path');
http.createServer(function (request, response) {
console.log('request starting...');
var filePath = '.' + request.url;
if (filePath == './')
filePath = './index.html';
var extname = path.extname(filePath);
var contentType = 'video/mp4';
fs.readFile(filePath, function(error, content) {
if (error) {
if(error.code == 'ENOENT'){
fs.readFile('./404.html', function(error, content) {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
});
}
else {
response.writeHead(500);
response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
response.end();
}
}
else {
response.writeHead(200, {
});
response.end(content, 'utf-8');
}
});
}).listen(8125);
console.log('Server running at http://127.0.0.1:8125/');
任何建议将不胜感激。
更新:
修改了 nodejs 代码。不过在 Safari 上仍然存在同样的问题:
var http = require('http');
var fs = require('fs');
http.createServer(function (request, response) {
console.log('request starting...');
var filePath = '.' + request.url;
if (filePath == './') {
var contentType = 'text/html';
filePath = './index.html';
} else {
var contentType = 'video/mp4';
}
var rstream = fs.createReadStream(filePath);
rstream.on('error', function(error) {
response.writeHead(404);
response.end();
});
rstream.on('open', function () {
response.writeHead(200, {
'Content-Type': contentType,
});
});
rstream.pipe(response);
}).listen(8125);
console.log('Server running at http://127.0.0.1:8125/');
还有用于 http 标头的 chrome 检查器:
【问题讨论】:
标签: node.js html html5-video