Merge pull request #71 from strongloop/feature/status-middleware

Add status middleware
This commit is contained in:
Ritchie Martori 2013-11-19 13:15:57 -08:00
commit 3753c15b71
2 changed files with 44 additions and 0 deletions

17
lib/middleware/status.js Normal file
View File

@ -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
});
}
}

View File

@ -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();
});
});
});
});