Database Reference
In-Depth Information
}
};
Finally, the endpoint itself retrieves the current counter state when the
connection starts and sends the counter data immediately. Then, the
handler for this connection attaches itself to the Counter object to obtain
future counter updates from the server, which the handler then passes along
to the client. The implementation of the handler below can be found in
dashboard/routes/index.js :
exports.dashboard = function(req,res) {
var counters = req.app.get('counters');
counters.state(function(err,data) {
console.log(data);
res.json(data,"counters");
});
function update(counter,value) {
var msg = {};msg[counter] = value;
res.json(msg,"counters");
}
counters.on("updated",update);
res.on("close",function() {
counters.removeListener("updated",update);
});
};
With everything in place, the dashboard should now respond to state
updates from the command line. Try starting the dashboard and then
executing the following commands from another window:
$ curl http://localhost:3000/api/v1/incr/
first?amount=25
OK
$ curl http://localhost:3000/api/v1/incr/
second?amount=25
OK
$ curl http://localhost:3000/api/v1/incr/
third?amount=50
OK
Search WWH ::




Custom Search