From 587efe2feee0e4059c8f3088bb98ceb8a641eec4 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Mon, 16 Jun 2014 08:44:52 -0700 Subject: [PATCH] Rename API variables for less confusion api.express => the Express instance api.server => the actual server (@express or a https.Server instance) --- lib/api.js | 26 +++++++++++++------------- test/specs/api.spec.js | 8 ++++---- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/api.js b/lib/api.js index 3c506af..84ce2be 100644 --- a/lib/api.js +++ b/lib/api.js @@ -29,14 +29,14 @@ var API = module.exports = function API(opts) { this.createServer(); - this.server.set('title', 'Cylon API Server'); + this.express.set('title', 'Cylon API Server'); - this.server.use(self.setupAuth()); - this.server.use(bodyParser()); - this.server.use(express["static"](__dirname + "/../node_modules/robeaux/")); + this.express.use(self.setupAuth()); + this.express.use(bodyParser()); + this.express.use(express["static"](__dirname + "/../node_modules/robeaux/")); // set CORS headers for API requests - this.server.use(function(req, res, next) { + this.express.use(function(req, res, next) { res.set("Access-Control-Allow-Origin", self.CORS || "*"); res.set("Access-Control-Allow-Headers", "Content-Type"); res.set('Content-Type', 'application/json'); @@ -44,7 +44,7 @@ var API = module.exports = function API(opts) { }); // extracts command params from request - this.server.use(function(req, res, next) { + this.express.use(function(req, res, next) { var method = req.method.toLowerCase(), container = {}; @@ -64,7 +64,7 @@ var API = module.exports = function API(opts) { }); // load route definitions - this.server.use('/', require('./api/routes')) + this.express.use('/', require('./api/routes')) }; API.prototype.defaults = { @@ -79,19 +79,19 @@ API.prototype.defaults = { }; API.prototype.createServer = function createServer() { - this.server = express(); + this.express = express(); //configure ssl if requested if (this.ssl && typeof(this.ssl) === 'object') { var https = require('https'); - this.server.node = https.createServer({ + this.server = https.createServer({ key: fs.readFileSync(this.ssl.key), cert: fs.readFileSync(this.ssl.cert) - }, this.server); + }, this.express); } else { Logger.warn("API using insecure connection. We recommend using an SSL certificate with Cylon.") - this.server.node = this.server; + this.server = this.express; } }; @@ -115,8 +115,8 @@ API.prototype.setupAuth = function setupAuth() { API.prototype.listen = function() { var self = this; - this.server.node.listen(this.port, this.host, null, function() { - var title = self.server.get('title'); + this.server.listen(this.port, this.host, null, function() { + var title = self.express.get('title'); var protocol = self.ssl ? "https" : "http"; Logger.info(title + " is now online."); diff --git a/test/specs/api.spec.js b/test/specs/api.spec.js index 6427075..4eb39da 100644 --- a/test/specs/api.spec.js +++ b/test/specs/api.spec.js @@ -34,13 +34,13 @@ describe("API", function() { expect(api.port).to.be.eql("3000") }); - it("sets @server to an Express server instance", function() { - expect(api.server).to.be.a('function'); + it("sets @express to an Express server instance", function() { + expect(api.express).to.be.a('function'); var methods = ['get', 'post', 'put', 'delete']; for (var i = 0; i < methods.length; i++) { - expect(api.server[methods[i]]).to.be.a('function'); + expect(api.express[methods[i]]).to.be.a('function'); } }); @@ -49,7 +49,7 @@ describe("API", function() { }); it("sets the server's title", function() { - var title = api.server.get('title'); + var title = api.express.get('title'); expect(title).to.be.eql("Cylon API Server"); });