Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node storing POSTs inside the Event Store
var express = require('express');
var app = express();
var http = require('http');
var uuid = require('node-uuid');
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on("exit", function (worker, code, signal) {
cluster.fork();
});
} else {
app.use(express.bodyParser());
app.post('/', function(request, response){
var events = [{
"eventId": uuid.v4(),
"eventType": "PushEvents",
"data": {
"events": request.body
}
}];
var body = JSON.stringify(events);
var req = http.request({
host: "127.0.0.1",
port: 2113,
path: "/streams/events",
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Content-Length": body.length,
"ES-ExpectedVersion": -2
}
}, function(res) {
// Do something with the response so that node closes the http connection
res.on('data', function() {
// do nothing
});
});
req.write(body);
req.end();
response.send(201);
});
app.listen(3000);
console.log('Listening on port 3000');
}
@geersch
Copy link
Author

geersch commented Mar 3, 2014

Node sample that listens for POST requests and stores the body of each request in the event store.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment