-
Notifications
You must be signed in to change notification settings - Fork 56
/
server.js
29 lines (25 loc) · 843 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var http = require('http')
, Report = require('./report');
/**
* This server will print the object upon request. The user should update the metrics
* as normal within their application.
*/
var Server = module.exports = function Server(port, trackedMetrics) {
var self = this;
this.report = new Report(trackedMetrics);
this.server = http.createServer(function (req, res) {
if (req.url.match(/^\/metrics/)) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(self.report.summary()));
} else {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Try hitting /metrics instead');
}
}).listen(port);
}
/**
* Adds a metric to be tracked by this server
*/
Server.prototype.addMetric = function (){
this.report.addMetric.apply(this.report, arguments);
}