diff --git a/lib/middleware/status.js b/lib/middleware/status.js new file mode 100644 index 00000000..02068548 --- /dev/null +++ b/lib/middleware/status.js @@ -0,0 +1,17 @@ +/** + * Export the middleware. + */ + +module.exports = status; + +function status() { + var started = new Date(); + + return function(req, res) { + res.send({ + started: started, + uptime: (Date.now() - Number(started)) / 1000 + }); + } +} + diff --git a/test/app.test.js b/test/app.test.js index 2d2b4814..bf47ff30 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -111,4 +111,31 @@ describe('app', function() { assert.isFunc(app.models.Foo, 'create'); }); }); + + describe('app.get("/", loopback.status())', function () { + it('should return the status of the application', function (done) { + var app = loopback(); + app.get('/', loopback.status()); + request(app) + .get('/') + .expect(200) + .end(function(err, res) { + if(err) return done(err); + + assert.equal(typeof res.body, 'object'); + assert(res.body.started); + assert(res.body.uptime); + + var elapsed = Date.now() - Number(new Date(res.body.started)); + + // elapsed should be a positive number... + assert(elapsed > 0); + + // less than 100 milliseconds + assert(elapsed < 100); + + done(); + }); + }); + }); });