Node storing POSTs inside the Event Store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Node sample that listens for POST requests and stores the body of each request in the event store.