~ubuntu-branches/ubuntu/vivid/node-negotiator/vivid

« back to all changes in this revision

Viewing changes to examples/encoding.js

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2013-10-20 17:33:37 UTC
  • Revision ID: package-import@ubuntu.com-20131020173337-75k05okhr2qhqcy1
Tags: upstream-0.3.0
Import upstream version 0.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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);