introduce express.Router( ), small renaming

Rename a few things here and there to help clarify the situation.

This change introduces use of express.Router( ), which provides the routing
functions of express without the full app.
This also shows an experiment observing the interaction betwen express( ) and
express.Router( ).
There are a few reasons for doing this:

Using express.Router( ) allows introspecting existing routes.
As a REST application, our content payloads should include URLs, links to other
resources available from our API.  As a bonus feature, it's often nice for a
meta page to describe the links, status, and version available from this
particular API.  By far, the easiest way to implement this is to introspect the
existing routes.

Separating the code makes testing easier.
This lays the ground work for breaking apart the code into smaller, more
testable modules.
This commit is contained in:
Ben West
2014-07-17 14:18:36 -07:00
parent 9cddb11bea
commit 5db7c2cfa1
+21 -5
View File
@@ -8,11 +8,12 @@ var sgvdata = require('sgvdata');
* API - Expose Nightscout HTTP API
* This api is designed to work with express.
*/
function api (env, entries, settings) {
function configure (env, entries, settings) {
// our globals
var express = require('express'),
api = express()
app = express( ),
api = express.Router()
;
// some middleware
@@ -22,7 +23,7 @@ function api (env, entries, settings) {
;
// set up express basics
api.set('title', 'Nightscout API v1');
app.set('title', 'Nightscout API v1');
// invoke common middleware
api.use(sendJSONStatus);
@@ -243,7 +244,22 @@ function api (env, entries, settings) {
});
});
return api;
function debug ( ) {
console.log('ROUTES', this.stack);
for (var k in this) {
console.log('KEY', k);
}
console.log(this.settings);
console.log(this.locals);
console.log("FOOBAR", this.get && this.get('foobar'))
}
app.set('foobar', "MY VALUE");
debug.call(api);
app.use(api);
debug.call(app);
return app;
}
module.exports = api;
module.exports = configure;