index.js
This file
Path
examples/online/index.js
61 lines · 1.0 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 2027b87 point at it.
fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
lines 1–61why, one step back1'use strict'23// install redis first:4// https://redis.io/56// then:7// $ npm install redis online8// $ redis-server910/**11 * Module dependencies.12 */1314var express = require('../..');15var online = require('online');16var redis = require('redis');17var db = redis.createClient();1819// online2021online = online(db);2223// app2425var app = express();2627// activity tracking, in this case using28// the UA string, you would use req.user.id etc2930app.use(function(req, res, next){31 // fire-and-forget32 online.add(req.headers['user-agent']);33 next();34});3536/**37 * List helper.38 */3940function list(ids) {41 return '<ul>' + ids.map(function(id){42 return '<li>' + id + '</li>';43 }).join('') + '</ul>';44}4546/**47 * GET users online.48 */4950app.get('/', function(req, res, next){51 online.last(5, function(err, ids){52 if (err) return next(err);53 res.send('<p>Users online: ' + ids.length + '</p>' + list(ids));54 });55});5657/* istanbul ignore next */58if (!module.parent) {59 app.listen(3000);60 console.log('Express started on port 3000');61}