简体   繁体   English

有人可以帮我在Node.Js中设置缓冲区吗?

[英]Can someone help me set up a buffer in Node.Js?

I am trying to load html5 video files onto the iPad. 我正在尝试将html5视频文件加载到iPad上。 I am using node.js. 我正在使用node.js。 Here is the code: 这是代码:

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

I'm really unsure what I'm doing here, and I need to make it stream/buffer. 我真的不确定我在这里做什么,我需要使其成为流/缓冲区。

The code seems to be an attempt to implement a connect middleware to serve static files. 该代码似乎是尝试实现连接中间件来提供静态文件的尝试。 Did you try to use standard connect middleware for this purpose? 您是否为此目的使用了标准连接中间件? Here is an example: 这是一个例子:

var connect = require('connect')
var server = connect.createServer(
    connect.logger()
  , connect.static(__dirname + '/public')
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM