Part of a presentation @ nodemtl meetup. Presenting Kerouac, a real-time webapp featuring a remote GPS tracking device, a webcam and a whole lot of Node.js magic covering some basics of Node.js such as: event emitters and process spawning.
10. Event Emitting
var events = require('events');
...
/* tap into prototype chain */
sys.inherits(Bancroft, events.EventEmitter);
...
var emitter = events.EventEmitter.call(this);
...
emitter.emit('location', self.location);
...
emitter.emit('error', error);
11. Event Handling
var bancroft = new Bancroft();
bancroft.on('connect', function () {
console.log('connected');
});
bancroft.on('location', function (location) {
console.log('got new location', location);
});
14. Init
var Cradle = require('cradle');
var cradle = new (Cradle.Connection)('http://127.0.0.1', 5984, {cache:
false});
var db = cradle.database('kerouac');
if (!db.exists()) {db.create();}
15. Store
var document = {
timestamp : new Date().getTime(),
location : bancroft.location
};
db.save(uuid(), document, function (err, res) {
console.log(res);
db.saveAttachment(res.id, res.rev,
'frame', 'image/png', frame,
function (err, data) {
...
});
});
23. Jade Templates
<?xml version="1.0" encoding="UTF-8"?>
kml(xmlns="http://earth.google.com/kml/2.1")
Document
name Newfoundland RoadTrip
description A dynamically waypoint selection from an ongoing road trip
Style(id="blueLine")
LineStyle
color red
width 4
Placemark
name Blue Line
styleUrl #blueLine
LineString
altitudeMode relative
coordinates
- if (coordinates.length)
- coordinates.forEach(function(coordinate) {
=coordinate
- })
24. REST Handling
app.get('/status', function (req, res) {
res.header('content-type', 'application/json');
var status = messaging.location;
db.view('kerouac/speed_mean', function (err, rows) {
if (rows[0]) {
status['mean'] = rows[0].value.toFixed(2);
} else {
status['mean'] = 'n.a.';
}
var json = JSON.stringify(status);
res.send(json);
});
});
26. Open Issues
for an upcoming meet-up? volunteers?
How to unit test event emission?
What about native c++ module?
What are the best practices regarding express.js code
organization?
How to distribute Socket.io/Express behind a proxy?