Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js cuts off files when serving over HTTPS

Tags:

node.js

I am trying to serve some JavaScript files with Node.js and for whatever reason the files are being cut off in the middle of the transmission. The code:

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

The files http://github.com/LearnBoost/Socket.IO/raw/master/socket.io.js and http://code.jquery.com/jquery-1.4.2.min.js. The first one is cut off at exactly 32KB and the second at exactly 16KB. This does not happen over HTTP, only HTTPS and only over a network (e.g.: not from localhost).

Any help would be really appreciated.

like image 613
ipartola Avatar asked Oct 11 '10 10:10

ipartola


People also ask

Do node JS servers block on HTTP requests?

Your code is non-blocking because it uses non-blocking I/O with the request() function. This means that node. js is free to service other requests while your series of http requests is being fetched.

Does node js support HTTPS?

HTTPS is the HTTP protocol over TLS/SSL. In Node. js this is implemented as a separate module.

How do I enable HTTPS in node JS?

To start your https server, run node app. js (here, app. js is name of the file) on the terminal. or in your browser, by going to https://localhost:8000 .

How does node js handle multiple HTTP requests?

How NodeJS handle multiple client requests? NodeJS receives multiple client requests and places them into EventQueue. NodeJS is built with the concept of event-driven architecture. NodeJS has its own EventLoop which is an infinite loop that receives requests and processes them.


2 Answers

Instead of Content-Length: data.length you should use Content-Length: Buffer.byteLength(data, 'utf8') where Bufferis a global object (node 0.3.x) or var Buffer = require('buffer') in node 0.2.x which will save you a lot of hassle and might fix your problem with truncated answers too.

like image 113
tedeh Avatar answered Oct 11 '22 19:10

tedeh


I have just seen this too. Same setup - HTTPS, latest node from the git repo.

One large file (170k) never properly completing sending. I tried to switch from async to synch but it made no difference. Only thing that fixed it so far was making the file smaller. It was a big floppy jpg so it was easy to do. Problem vanished.

like image 20
davidthings Avatar answered Oct 11 '22 21:10

davidthings