Rename API variables for less confusion

api.express => the Express instance
api.server  => the actual server (@express or a https.Server instance)
This commit is contained in:
Andrew Stewart 2014-06-16 08:44:52 -07:00
parent b49c2da3f1
commit 587efe2fee
2 changed files with 17 additions and 17 deletions

View File

@ -29,14 +29,14 @@ var API = module.exports = function API(opts) {
this.createServer(); this.createServer();
this.server.set('title', 'Cylon API Server'); this.express.set('title', 'Cylon API Server');
this.server.use(self.setupAuth()); this.express.use(self.setupAuth());
this.server.use(bodyParser()); this.express.use(bodyParser());
this.server.use(express["static"](__dirname + "/../node_modules/robeaux/")); this.express.use(express["static"](__dirname + "/../node_modules/robeaux/"));
// set CORS headers for API requests // 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-Origin", self.CORS || "*");
res.set("Access-Control-Allow-Headers", "Content-Type"); res.set("Access-Control-Allow-Headers", "Content-Type");
res.set('Content-Type', 'application/json'); res.set('Content-Type', 'application/json');
@ -44,7 +44,7 @@ var API = module.exports = function API(opts) {
}); });
// extracts command params from request // extracts command params from request
this.server.use(function(req, res, next) { this.express.use(function(req, res, next) {
var method = req.method.toLowerCase(), var method = req.method.toLowerCase(),
container = {}; container = {};
@ -64,7 +64,7 @@ var API = module.exports = function API(opts) {
}); });
// load route definitions // load route definitions
this.server.use('/', require('./api/routes')) this.express.use('/', require('./api/routes'))
}; };
API.prototype.defaults = { API.prototype.defaults = {
@ -79,19 +79,19 @@ API.prototype.defaults = {
}; };
API.prototype.createServer = function createServer() { API.prototype.createServer = function createServer() {
this.server = express(); this.express = express();
//configure ssl if requested //configure ssl if requested
if (this.ssl && typeof(this.ssl) === 'object') { if (this.ssl && typeof(this.ssl) === 'object') {
var https = require('https'); var https = require('https');
this.server.node = https.createServer({ this.server = https.createServer({
key: fs.readFileSync(this.ssl.key), key: fs.readFileSync(this.ssl.key),
cert: fs.readFileSync(this.ssl.cert) cert: fs.readFileSync(this.ssl.cert)
}, this.server); }, this.express);
} else { } else {
Logger.warn("API using insecure connection. We recommend using an SSL certificate with Cylon.") 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() { API.prototype.listen = function() {
var self = this; var self = this;
this.server.node.listen(this.port, this.host, null, function() { this.server.listen(this.port, this.host, null, function() {
var title = self.server.get('title'); var title = self.express.get('title');
var protocol = self.ssl ? "https" : "http"; var protocol = self.ssl ? "https" : "http";
Logger.info(title + " is now online."); Logger.info(title + " is now online.");

View File

@ -34,13 +34,13 @@ describe("API", function() {
expect(api.port).to.be.eql("3000") expect(api.port).to.be.eql("3000")
}); });
it("sets @server to an Express server instance", function() { it("sets @express to an Express server instance", function() {
expect(api.server).to.be.a('function'); expect(api.express).to.be.a('function');
var methods = ['get', 'post', 'put', 'delete']; var methods = ['get', 'post', 'put', 'delete'];
for (var i = 0; i < methods.length; i++) { 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() { 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"); expect(title).to.be.eql("Cylon API Server");
}); });