encoding.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. (function() {
  2. var Negotiator, gbuf, http, messages;
  3. Negotiator = require('../lib/negotiator').Negotiator;
  4. http = require('http');
  5. gbuf = require('gzip-buffer');
  6. messages = {
  7. identity: 'Hello World'
  8. };
  9. gbuf.gzip(messages.identity, function(zipped) {
  10. var availableEncodings, key, server, val;
  11. messages.gzip = zipped;
  12. availableEncodings = (function() {
  13. var _results;
  14. _results = [];
  15. for (key in messages) {
  16. val = messages[key];
  17. _results.push(key);
  18. }
  19. return _results;
  20. })();
  21. console.log(availableEncodings);
  22. server = http.createServer(function(req, res) {
  23. var encoding, negotiator;
  24. negotiator = new Negotiator(req);
  25. console.log("Accept-Encoding: " + req.headers['accept-encoding']);
  26. console.log("Preferred: " + (negotiator.preferredEncodings()));
  27. console.log("Possible: " + (negotiator.preferredEncodings(availableEncodings)));
  28. encoding = negotiator.preferredEncoding(availableEncodings);
  29. console.log("Selected: " + encoding);
  30. if (encoding) {
  31. res.writeHead(200, {
  32. 'Content-Encoding': encoding
  33. });
  34. return res.end(messages[encoding]);
  35. } else {
  36. res.writeHead(406);
  37. return res.end();
  38. }
  39. });
  40. return server.listen(8080);
  41. });
  42. }).call(this);