Last active
December 14, 2015 17:09
-
-
Save ninjascribble/5120308 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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