0

I have a nodejs server, and I would like to gzip the html. I try to used [this post][1] has David suggested to gzip html. But I don't see the html being gziped in chrome, and on top of that, it crash Error: ENOENT: no such file or directory, open './public/js/vendor/plyr.min.js.map'

Plyr was working fine without before.

    var http = require('http');
    var fs = require('fs');
    var path = require('path');
    var url = require('url');
    var port = process.env.PORT || 1881; 

    http.createServer(function (request, response) {
        var filePath = '.' + request.url;
        if (filePath == './')
           filePath = './public/index.html';

        var extname = path.extname(filePath);
        var contentType = 'text/html';

var raw = fs.createReadStream(filePath);
    var acceptEncoding = request.headers['accept-encoding'];
    if (!acceptEncoding) {
        acceptEncoding = '';
    }

    // Note: this is not a conformant accept-encoding parser.
    // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
    if (acceptEncoding.match(/\bdeflate\b/)) {
        response.writeHead(200, { 'content-encoding': 'deflate' });
        raw.pipe(zlib.createDeflate()).pipe(response);
    } else if (acceptEncoding.match(/\bgzip\b/)) {
        response.writeHead(200, { 'content-encoding': 'gzip' });
        raw.pipe(zlib.createGzip()).pipe(response);
    } else {
        response.writeHead(200, {});
        raw.pipe(response);
    }

        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, { 'Content-Type': contentType });
                response.end(content, 'utf-8');
            }
        });
    }).listen(port);
    console.log("Server Running on "+port+".\nLaunch http://localhost:"+port);


  [1]: https://stackoverflow.com/questions/3894794/node-js-gzip-compression
Rom
  • 109
  • 2
  • 13
  • Possible duplicate of [Node.js: Gzip compression?](https://stackoverflow.com/questions/3894794/node-js-gzip-compression) – davidonet Oct 10 '18 at 14:20
  • Could, be, so I updated my file, but now, I don't see me html gziped in chrome inspector, and it keep crashing with : Error: ENOENT: no such file or directory, open './public/js/vendor/plyr.min.js.map'. I never had the use of this file before. – Rom Oct 10 '18 at 15:16

1 Answers1

0

Ok, so i was stupid again yesterday :) I should have put the compressing part in the fs.readFile function. It works now.

Rom
  • 109
  • 2
  • 13