From 4b0bf68362b81f131a4fa51273a40570b4bcd608 Mon Sep 17 00:00:00 2001 From: BryceCicada Date: Tue, 17 May 2016 22:50:50 +0100 Subject: [PATCH 1/4] Allow SSL/TLS connections --- lib/jsonApi.js | 9 ++++++++- lib/router.js | 5 +++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/jsonApi.js b/lib/jsonApi.js index 8774064a..df58a2ed 100644 --- a/lib/jsonApi.js +++ b/lib/jsonApi.js @@ -107,7 +107,14 @@ jsonApi.onUncaughtException = function(errHandler) { jsonApi.start = function() { routes.register(); - router.listen(jsonApi._apiConfig.port); + var serverFactory = function(app) { + if (jsonApi._apiConfig.protocol === 'https') { + return require('https').createServer(jsonApi._apiConfig.tls || {}, app); + } else { + return require('http').createServer(app); + } + }; + router.listen(jsonApi._apiConfig.port, serverFactory); }; jsonApi.close = function() { diff --git a/lib/router.js b/lib/router.js index 6c026b12..7ddea4d5 100644 --- a/lib/router.js +++ b/lib/router.js @@ -78,9 +78,10 @@ app.route("*").all(function(req, res, next) { next(); }); -router.listen = function(port) { +router.listen = function(port, serverFactory) { if (!server) { - server = app.listen(port); + server = serverFactory(app); + server.listen(port); } }; From 02f44ab10c9047e3eed5bdd0d7cff836536c481e Mon Sep 17 00:00:00 2001 From: BryceCicada Date: Tue, 17 May 2016 23:44:39 +0100 Subject: [PATCH 2/4] Fixed lint warnings --- lib/jsonApi.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/jsonApi.js b/lib/jsonApi.js index df58a2ed..ace2a50f 100644 --- a/lib/jsonApi.js +++ b/lib/jsonApi.js @@ -108,10 +108,10 @@ jsonApi.onUncaughtException = function(errHandler) { jsonApi.start = function() { routes.register(); var serverFactory = function(app) { - if (jsonApi._apiConfig.protocol === 'https') { - return require('https').createServer(jsonApi._apiConfig.tls || {}, app); + if (jsonApi._apiConfig.protocol === "https") { + return require("https").createServer(jsonApi._apiConfig.tls || {}, app); } else { - return require('http').createServer(app); + return require("http").createServer(app); } }; router.listen(jsonApi._apiConfig.port, serverFactory); From 02ec9d7e7d535f0508f206368d7193c62b86730a Mon Sep 17 00:00:00 2001 From: Chris Nix Date: Thu, 19 May 2016 21:06:46 +0100 Subject: [PATCH 3/4] Added documentation --- documentation/configuring.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/documentation/configuring.md b/documentation/configuring.md index adc3e41d..98e197b6 100644 --- a/documentation/configuring.md +++ b/documentation/configuring.md @@ -19,6 +19,40 @@ jsonApi.setConfig({ }); ``` +#### Configuring HTTPS + +To run over HTTPS, set the protocol to _https_ and configure the appropriate TLS settings + +For example: + +```javascript +var fs = require("fs"); +jsonApi.setConfig({ + protocol: "https", + port: 16006, + tls: { + cert: fs.readFileSync('server.crt'), + key: fs.readFileSync('server.key') + passphrase: 'pass' + } +}); +``` +or + +```javascript +var fs = require("fs"); +jsonApi.setConfig({ + protocol: "https", + port: 16006, + tls: { + pfx: fs.readFileSync('server.pfx'), + passphrase: 'pass' + } +}); +``` + +For a full set of tls options, see https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener + #### Error Handling ```javascript From dec201569bbd718ce9000372ef94e40371f84880 Mon Sep 17 00:00:00 2001 From: Chris Nix Date: Mon, 23 May 2016 13:25:13 +0100 Subject: [PATCH 4/4] Refactored server creation into router.js --- lib/jsonApi.js | 9 +-------- lib/router.js | 9 +++++++-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/jsonApi.js b/lib/jsonApi.js index ace2a50f..8774064a 100644 --- a/lib/jsonApi.js +++ b/lib/jsonApi.js @@ -107,14 +107,7 @@ jsonApi.onUncaughtException = function(errHandler) { jsonApi.start = function() { routes.register(); - var serverFactory = function(app) { - if (jsonApi._apiConfig.protocol === "https") { - return require("https").createServer(jsonApi._apiConfig.tls || {}, app); - } else { - return require("http").createServer(app); - } - }; - router.listen(jsonApi._apiConfig.port, serverFactory); + router.listen(jsonApi._apiConfig.port); }; jsonApi.close = function() { diff --git a/lib/router.js b/lib/router.js index 7ddea4d5..92223fef 100644 --- a/lib/router.js +++ b/lib/router.js @@ -78,9 +78,14 @@ app.route("*").all(function(req, res, next) { next(); }); -router.listen = function(port, serverFactory) { +router.listen = function(port) { + if (!server) { - server = serverFactory(app); + if (jsonApi._apiConfig.protocol === "https") { + server = require("https").createServer(jsonApi._apiConfig.tls || {}, app); + } else { + server = require("http").createServer(app); + } server.listen(port); } };