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