Skip to content

Instantly share code, notes, and snippets.

@olauzon
Created December 16, 2011 18:01
Show Gist options
  • Save olauzon/1487162 to your computer and use it in GitHub Desktop.
Save olauzon/1487162 to your computer and use it in GitHub Desktop.
useragent module examples
var http = require('http'),
userAgent = require('useragent');
http.createServer(function (request, response) {
var userAgentString = request.headers['user-agent'],
ua_obj = userAgent.parse(userAgentString),
is = userAgent.is(userAgentString),
content = "";
content += "<h1>{ua}</h1>";
content += "<dl>";
content += "<dt>userAgent.is</dt><dd>{is}</dd>";
content += "<dt>userAgent.parse</dt><dd>{ua_obj}</dd>";
content += "<dt>userAgent response.pretty</dt><dd>{pretty}</dd>";
content += "</dl>";
response.writeHead( 200, {'Content-Type': 'text/html'});
response.end(
content.replace("{ua}", userAgentString)
.replace("{ua_obj}", JSON.stringify(ua_obj))
.replace("{is}", JSON.stringify(is))
.replace("{pretty}", ua_obj.toAgent())
);
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment