Skip to content

Instantly share code, notes, and snippets.

@itayw
Last active August 29, 2015 14:10
Show Gist options
  • Save itayw/683c0f7ba7739dd3c6ca to your computer and use it in GitHub Desktop.
Save itayw/683c0f7ba7739dd3c6ca to your computer and use it in GitHub Desktop.
Advanced Node.JS Meetup Examples
var express = require('express');
var useragent = require('useragent');
var app = express();
useragent(true);
app.use(function (req, res, next) {
var agent = useragent.parse(req.headers['user-agent']);
req.joola_event = {
timestamp: new Date(),
event: 'request',
agent: agent,
os: agent.os,
device: agent.device,
ip: req.ip,
path: req.path
};
return next();
});
app.get('/', function (req, res, next) {
res.send('hello world');
return next();
});
app.use(function (req, res, next) {
req.joola_event.duration_ms = new Date().getTime() - req.joola_event.timestamp.getTime();
console.log(req.joola_event);
res.end();
return next();
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port)
});
<html>
<head>
<script src="http://localhost:8080/joola.js?APIToken=apitoken-demo&debug.enabled=true"></script>
</head>
<body>
<div id="timeline"></div>
<script>
joola.on('ready', function (err) {
if (err)
throw err;
console.log('joola ready');
$('#timeline').Timeline({
chart: {
chart: {
type: 'line'
}
},
query: {
timeframe: 'last_30_minutes',
interval: 'minute',
dimensions: ['timestamp'],
metrics: ['count', {key: 'duration_ms', aggregation: 'avg'}],
collection: 'request',
realtime: true
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment