Permalink
Browse files

done

  • Loading branch information...
1 parent adebb33 commit 09cc319168a3c305991bf9417309dc7c09c80067 @tj committed Oct 2, 2011
Showing with 65 additions and 2 deletions.
  1. +29 −0 Readme.md
  2. +15 −0 examples/index.js
  3. +21 −2 lib/connect-useragent.js
View
@@ -3,6 +3,35 @@
A small Connect user-agent middleware exposing user-agent details to your application and views. A good idea by @guille backed by @3rd-Eden's [useragent](https://github.com/3rd-Eden/useragent) module.
+## Installation
+
+ $ npm install connect-useragent
+
+## Example
+
+```js
+var connect = require('connect')
+ , useragent = require('connect-useragent');
+
+connect()
+ .use(connect.logger('dev'))
+ .use(useragent())
+ .use(function(req, res){
+ console.log(req.agent);
+ })
+ .listen(3000);
+```
+
+provides details such as the following:
+
+```js
+{ family: 'Chrome',
+ major: '16',
+ minor: '0',
+ patch: '891',
+ os: 'Mac OS X' }
+```
+
## License
(The MIT License)
View
@@ -0,0 +1,15 @@
+
+/**
+ * Module dependencies.
+ */
+
+var connect = require('connect')
+ , useragent = require('../');
+
+connect()
+ .use(connect.logger('dev'))
+ .use(useragent())
+ .use(function(req, res){
+ console.log(req.agent);
+ })
+ .listen(3000);
View
@@ -6,7 +6,26 @@
*/
/**
- * Library version.
+ * Module dependencies.
*/
-exports.version = '0.0.1';
+var ua = require('useragent');
+
+/**
+ * Expose the middleware.
+ */
+
+module.exports = useragent;
+
+/**
+ * Parse the user-agent string when available and `next()`.
+ *
+ * @api public
+ */
+
+function useragent() {
+ return function(req, res, next){
+ req.agent = ua.parse(req.headers['user-agent'] || '');
+ next();
+ }
+}

0 comments on commit 09cc319

Please sign in to comment.