我正在尝试将html5视频文件加载到iPad上。我正在使用node.js.这是代码:
return function staticProvider(req, res, next) {
if (req.method != 'GET' && req.method != 'HEAD') return next();
var hit,
head = req.method == 'HEAD',
filename, url = parseUrl(req.url);
// Potentially malicious path
if (~url.pathname.indexOf('..')) {
console.log("forbidden", url.pathname);
return forbidden(res);
}
// Absolute path
filename = Path.join(root, queryString.unescape(url.pathname));
// Index.html support
if (filename[filename.length - 1] === '/') {
filename += "index.html";
}
// Cache hit
if (cache && !conditionalGET(req) && (hit = _cache[req.url])) {
res.writeHead(200, hit.headers);
res.end(head ? undefined : hit.body);
return;
}
fs.stat(filename, function(err, stat){
// Pass through for missing files, thow error for other problems
if (err) {
return err.errno === process.ENOENT
? next()
: next(err);
} else if (stat.isDirectory()) {
return next();
}
// Serve the file directly using buffers
function onRead(err, data) {
if (err) return next(err);
// Response headers
var headers = {
"Content-Type": mime.lookup(filename),
"Content-Length": stat.size,
"Last-Modified": stat.mtime.toUTCString(),
"Cache-Control": "public max-age=" + (maxAge / 1000),
"ETag": etag(stat),
"Accept-Ranges": "bytes"
};
// Conditional GET
if (!modified(req, headers)) {
return notModified(res, headers);
}
res.writeHead(200, headers);
res.end(head ? undefined : data);
// Cache support
if (cache) {
_cache[req.url] = {
headers: headers,
body: data
};
}
}
fs.readFile(filename, onRead);
});
};
};
我真的不确定我在这里做什么,我需要让它成为流/缓冲区。
答案 0 :(得分:1)
代码似乎是尝试实现连接中间件来提供静态文件。您是否尝试使用标准连接中间件来实现此目的?这是一个例子:
var connect = require('connect')
var server = connect.createServer(
connect.logger()
, connect.static(__dirname + '/public')
)