Skip to content

Instantly share code, notes, and snippets.

@AndyStewart
Last active August 29, 2015 14:01
Show Gist options
  • Save AndyStewart/ff21cae5aab27f2437c2 to your computer and use it in GitHub Desktop.
Save AndyStewart/ff21cae5aab27f2437c2 to your computer and use it in GitHub Desktop.
Reading from eventStore using node.js
var request = require('request');
var async = require('async');
function eventStore() {
function createEvent(event, body) {
return {
data: body,
type: event.summary
};
}
function getEvent(event, callback) {
var requestOptions = optionBuilder(event.id);
request(requestOptions, function(error, response, body) {
return callback(error, createEvent(event, body));
});
}
function getEventsInStream(streamName, callback) {
var requestOptions = optionBuilder("http://localhost:2113/streams/" + streamName);
request(requestOptions, function(error, response, body) {
async.map(body.entries, getEvent, callback);
});
}
return {
getEventsInStream: getEventsInStream
};
}
var optionBuilder = function(url) {
var host = url;
var options = {
json: true,
'auth': {
'user': 'admin',
'pass': 'changeit',
'sendImmediately': false
},
url: url,
};
return options;
};
module.exports = eventStore();
var eventStore = require('./eventStore');
exports.index = function(req, res){
var id = req.params.id;
eventStore.getEventsInStream("esubs-Guest-form-" + req.params.id, function(err, events) {
res.send({history: events });
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment