https://github.com/primaryobjects/usersonline
List the most recent 100 users visiting your site in node.js Express: Date, Time, IP Address, User Agent, Landing Page, Referring Url.
https://github.com/primaryobjects/usersonline
Last synced: 10 months ago
JSON representation
List the most recent 100 users visiting your site in node.js Express: Date, Time, IP Address, User Agent, Landing Page, Referring Url.
- Host: GitHub
- URL: https://github.com/primaryobjects/usersonline
- Owner: primaryobjects
- License: mit
- Created: 2013-04-23T15:45:36.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2016-01-14T16:41:01.000Z (about 10 years ago)
- Last Synced: 2024-10-29T22:50:29.013Z (about 1 year ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
UsersOnline
--------
List the most recent 100 users visiting your site and display the date, time, IP address, user agent, landing page, referring url.
This module tracks each new user that first visits your site, by activating upon the start of a new session. When a new session is activated, the user's referring url information is added to a queue. You can output the queue in your view, such as on an administrator page, to get a quick view of the users accessing your site.
For example:
```
1. 4/23/2013 11:42:07 AM - 127.0.0.1
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
landing: http://www.primaryobjects.com/users
referer: http://www.google.com/search?q=software+design&aq=0&oq=software+design
```
## Usage
Install the node.js module.
```bash
$ npm install usersonline
```
## app.js
```
var session = require('express-session'),
usersonline = require('usersonline');
var app = express();
app.use(session({ secret: 'secret-session-key', resave: false, saveUninitialized: false })); // Enable session.
app.use(usersonline.logger); // Enable usersonline.
app.get('/users', function(req, res, next) {
res.json(usersonline.visitorList);
});
```
Note, if you are using express.static() for static files, the line `app.use(usersonline.logger)` must come before it.
To display the users online in a static HTML page, simply make a request to /users and display the resulting JSON, as follows:
## users.html
```
function getUsers() {
$.get('/users', function(data) {
for (var i in data) {
var visitor = data[i];
var status = $('#status');
status.append((parseInt(i) + 1) + '. ' + visitor.date + ' - ' + visitor.ip + '<br>');
status.append(visitor.userAgent + '<br>');
status.append('landing: ');
status.append("<a href='" + visitor.url + "', target='_blank'>" + visitor.url + "</a><br>");
if (visitor.referer != undefined) {
status.append('referer: ');
status.append("<a href='" + visitor.referer + "', target='_blank'>" + visitor.referer + "</a><br>");
}
status.append('<br>');
}
});
}
$(document).ready(function() { getUsers(); });
Users Online
```
If you are using express routing, you can display the UsersOnline list in your view route, as follows:
## index.js
```
var usersonline = require('usersonline');
exports.index = function(req, res) {
res.render('index', { visitorList: usersonline.visitorList });
};
```
## index.jade
```
each visitor, i in visitorList
p
div
| #{i + 1}. #{visitor.date} - #{visitor.ip}
div
| #{visitor.userAgent}
div
| landing:
a(href='#{visitor.url}', target='_blank') #{visitor.url}
div
if (visitor.referer != undefined)
| referer:
a(href='#{visitor.referer}', target='_blank') #{visitor.referer}
```
## index.ejs
```
<%
var content = '';
for (var i=0; i' + (i + 1) + '. ' + visitorList[i].date + ' - ' + visitorList[i].ip + '
';
content += visitorList[i].userAgent + '
landing: http://www.primaryobjects.com' + visitorList[i].url + '';
content += (visitorList[i].referer == undefined ? '' : '
referer: ' + visitorList[i].referer + '');
content += '';
myTemplateBlock = content;
}
%>
```
## Source
https://github.com/primaryobjects/usersonline
## Author
Kory Becker
http://www.primaryobjects.com