Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // server example
- // Running a gzip operation on every request is quite expensive.
- // It would be much more efficient to cache the compressed buffer.
- var zlib = require('zlib');
- var http = require('http');
- var fs = require('fs');
- http.createServer(function(request, response) {
- var raw = fs.createReadStream('index.html');
- 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);
- }
- }).listen(1337);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement