Skip to content

Instantly share code, notes, and snippets.

@ninjascribble
Last active December 14, 2015 17:09
Show Gist options
  • Save ninjascribble/5120308 to your computer and use it in GitHub Desktop.
Save ninjascribble/5120308 to your computer and use it in GitHub Desktop.
/** NodeJS with Express */
var express = require('express')
, http = require('http')
, app = express();
http.createServer(app).listen(3000);
app.get('/', function(req, res) {
console.log(req.get('user-agent'));
});
/** User-agent pattern matching */
var browser = 'unknown'
, version = 'unknown'
, patterns = {
android: /android\s([0-9.]*)/i
chrome: /chrome\/([0-9.]*)/i,
firefox: /firefox\/([0-9.]*)/i,
ie: /msie\s([0-9.]*)/i,
ipad: /ipad.version\/([0-9.]*).safari/i,
iphone_4: /iphone\sos\s4.version.mobile\/([0-9.]*).safari/i,
iphone_5: /iphone\sos\s5.version.mobile\/([0-9.]*).safari/i,
safari: /version\/([0-9.]*).safari/i
}
, key, matches;
for (key in patterns) {
matches = navigator.userAgent.match(patterns[key]);
if (matches) {
browser = key;
version = matches[1];
break;
}
}
console.log(browser, version);
/** Native NodeJS */
var http = require('http')
, server = http.createServer(function(req) {
console.log(req.headers['user-agent']);
});
server.listen(3000, 'localhost');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment