Node.js在通过HTTPS提供服务时会切断文件

ipa*_*ola 6 node.js

我试图用Node.js提供一些JavaScript文件,无论出于何种原因,文件在传输过程中被切断.代码:

httpsServer = http.createServer(function(req, res) {
    var path = url.parse(req.url).pathname;

    if (path[path.length - 1] == '/') {
            path += 'index.html';
    }

    fs.readFile(root + path, function(err, data){
            if (err) return send404(res);

            res.writeHead(200, {
                'Content-Type': getMimeType(getExtension(path)),
                'Content-Length': data.length});
            res.end(data);

    });
}),

var privateKey = fs.readFileSync(settings.PRIVATE_KEY).toString();
var certificate = fs.readFileSync(settings.CERTIFICATE).toString();

var credentials = crypto.createCredentials({key: privateKey, cert: certificate});
httpsServer.setSecure(credentials);
httpsServer.listen(settings.HTTPS_PORT);
Run Code Online (Sandbox Code Playgroud)

文件http://github.com/LearnBoost/Socket.IO/raw/master/socket.io.jshttp://code.jquery.com/jquery-1.4.2.min.js.第一个是正确的32KB,第二个正好是16KB.这不会通过HTTP发生,只会通过HTTPS发生,而且只能通过网络发生(例如:不是来自localhost).

任何帮助将非常感激.

ted*_*deh 12

相反的Content-Length: data.length,你应该使用Content-Length: Buffer.byteLength(data, 'utf8')这里Buffer是一个全局对象(节点0.3.X),或var Buffer = require('buffer')在节点的0.2.x这将节省你很多的麻烦,并可能修复被截断的回答你的问题了.