Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. // server example
  2. // Running a gzip operation on every request is quite expensive.
  3. // It would be much more efficient to cache the compressed buffer.
  4. var zlib = require('zlib');
  5. var http = require('http');
  6. var fs = require('fs');
  7. http.createServer(function(request, response) {
  8. var raw = fs.createReadStream('index.html');
  9. var acceptEncoding = request.headers['accept-encoding'];
  10. if (!acceptEncoding) {
  11. acceptEncoding = '';
  12. }
  13.  
  14. // Note: this is not a conformant accept-encoding parser.
  15. // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
  16. if (acceptEncoding.match(/\bdeflate\b/)) {
  17. response.writeHead(200, { 'content-encoding': 'deflate' });
  18. raw.pipe(zlib.createDeflate()).pipe(response);
  19. } else if (acceptEncoding.match(/\bgzip\b/)) {
  20. response.writeHead(200, { 'content-encoding': 'gzip' });
  21. raw.pipe(zlib.createGzip()).pipe(response);
  22. } else {
  23. response.writeHead(200, {});
  24. raw.pipe(response);
  25. }
  26. }).listen(1337);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement