Skip to content

Commit

Permalink
Revert #55, closes #46. Bind to both HTTP and HTTPs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Feb 3, 2017
1 parent ea97e78 commit b828ccd
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 67 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

### 2.3.2 (Next)

* [#55](https://github.com/alexa-js/alexa-app-server/pull/55): Don't bind to both HTTP and HTTPs, simplify HTTPs options - [@dblock](https://github.com/dblock).
* [#48](https://github.com/alexa-js/alexa-app-server/pull/48): Added more node versions to run mocha tests on Travis-CI - [@tejashah88](https://github.com/tejashah88).
* [#48](https://github.com/alexa-js/alexa-app-server/pull/48): Removed deprecated dependency 'supertest-as-promised' - [@tejashah88](https://github.com/tejashah88).
* [#48](https://github.com/alexa-js/alexa-app-server/pull/48): Added tests to verify that server does not always bind to both HTTP and HTTPS ports [#46](https://github.com/alexa-js/alexa-app-server/issues/46) - [@tejashah88](https://github.com/tejashah88).
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,11 @@ require('alexa-app-server').start({
// The value passed on by the promise (if any) replaces the response json.
postRequest : function(json, request, response) { },

// Enables https support. Note privateKey, and certificate are required.
https: true,
// Enables https support. Note httpsPort, privateKey, and certificate are required.
httpsEnabled: true,

// The https port the server will bind to. Required for httpsEnabled support.
httpsPort: 443,

// Specifies the private key filename. This file must reside in the sslcert folder under the
// root of the project.
Expand Down Expand Up @@ -189,8 +192,8 @@ Add the following properties the to config that creates the server.

```javascript
AlexaAppServer.start({
port: 443,
https: true,
httpsPort: 443,
httpsEnabled: true,
privateKey: 'private-key.pem',
certificate: 'cert.cer'
});
Expand Down
6 changes: 1 addition & 5 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@

### Upgrading to >= Next

#### Changes in HTTPs support

To enable HTTPs use `https: true` instead of `httpsEnabled: true`. Furthermore, `httsPort` is deprecated, the server will no longer bind to both HTTP and HTTPs and will bind to `port` in both cases.

See [#55](https://github.com/alexa-js/alexa-app-server/pull/55) for more information.
TODO
28 changes: 14 additions & 14 deletions examples/server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var AlexaAppServer = require("../index.js");
AlexaAppServer.start( {
server_root: './',
port:8080
// Use preRequest to load user data on each request and add it to the request json.
// In reality, this data would come from a db or files, etc.
,preRequest: function(json,req,res) {
console.log("preRequest fired");
json.userDetails = { "name":"Bob Smith" };
}
// Add a dummy attribute to the response
,postRequest: function(json,req,res) {
json.dummy = "text";
}
} );
AlexaAppServer.start({
server_root: './',
port: 8080,
// Use preRequest to load user data on each request and add it to the request json.
// In reality, this data would come from a db or files, etc.
preRequest: function(json, req, res) {
console.log("preRequest fired");
json.userDetails = { "name": "Bob Smith" };
},
// Add a dummy attribute to the response
postRequest: function(json, req, res) {
json.dummy = "text";
}
});
36 changes: 20 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,10 @@ var appServer = function(config) {
self.log("apps not loaded because directory [" + app_dir + "] does not exist");
}

config.port = config.port || process.env.port || 8080;

if (config.https == true) {
if (config.httpsEnabled == true) {
self.log("enabling https");

if (config.privateKey != undefined && config.certificate != undefined) { // Ensure that all of the needed properties are set
if (config.privateKey != undefined && config.certificate != undefined && config.httpsPort != undefined) { // Ensure that all of the needed properties are set
var privateKeyFile = server_root + '/sslcert/' + config.privateKey;
var certificateFile = server_root + '/sslcert/' + config.certificate;
var chainFile = (config.chain != undefined) ? server_root + '/sslcert/' + config.chain : undefined; //optional chain bundle
Expand Down Expand Up @@ -267,11 +265,11 @@ var appServer = function(config) {
// TODO: add separate option to specify specific host address for HTTPS server to bind to???
// Issue #38: https://github.com/alexa-js/alexa-app-server/issues/38
if (typeof config.host === 'string') {
self.instance = httpsServer.listen(config.port, config.host);
self.log("listening on https://" + config.host + ":" + config.port);
self.httpsInstance = httpsServer.listen(config.httpsPort, config.host);
self.log("listening on https://" + config.host + ":" + config.httpsPort);
} else {
self.instance = httpsServer.listen(config.port);
self.log("listening on https port " + config.port);
self.httpsInstance = httpsServer.listen(config.httpsPort);
self.log("listening on https port " + config.httpsPort);
}
} catch (error) {
self.error("failed to listen via https: " + error);
Expand All @@ -283,16 +281,18 @@ var appServer = function(config) {
self.error("privateKey: '" + config.privateKey + "' or certificate: '" + config.certificate + "' do not exist in /sslcert, https will not be enabled");
}
} else {
self.error("privatekey or certificate parameter is not set in config, https will not be enabled");
self.error("httpsPort, privateKey or certificate parameter is not set in config, https will not be enabled");
}
}

config.port = config.port || process.env.port || 8080;

if (typeof config.host === 'string') {
self.instance = self.express.listen(config.port, config.host);
self.log("listening on http://" + config.host + ":" + config.port);
} else {
if (typeof config.host === 'string') {
self.instance = self.express.listen(config.port, config.host);
self.log("listening on http://" + config.host + ":" + config.port);
} else {
self.instance = self.express.listen(config.port);
self.log("listening on http port " + config.port);
}
self.instance = self.express.listen(config.port);
self.log("listening on http port " + config.port);
}

// Run the post() method if defined
Expand All @@ -309,6 +309,10 @@ var appServer = function(config) {
self.instance.close();
}

if (typeof self.httpsInstance !== "undefined") {
self.httpsInstance.close();
}

// deactivate all hotswap listener
hotswap.removeListener('swap', hotswapCallback);
hotswap.removeListener('error', errorCallback);
Expand Down
8 changes: 4 additions & 4 deletions test/test-examples-server-custom-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ describe("Alexa App Server with Examples & Custom Server Bindings", function() {

it("mounts the hello world app (HTTP & HTTPS) (CA chain file not included) and bind to the specified address", function() {
testServer = alexaAppServer.start({
port: 6000,
httpsPort: 6000,
host: "127.0.0.1",
server_root: 'examples',
https: true,
httpsEnabled: true,
privateKey: 'private-key.pem',
certificate: 'cert.cer',
passphrase: 'test123'
Expand All @@ -50,10 +50,10 @@ describe("Alexa App Server with Examples & Custom Server Bindings", function() {

it("mounts the hello world app (HTTP & HTTPS) (CA chain file included) and bind to the specified address", function() {
testServer = alexaAppServer.start({
port: 6000,
httpsPort: 6000,
host: "127.0.0.1",
server_root: 'examples',
https: true,
httpsEnabled: true,
privateKey: 'private-key.pem',
certificate: 'cert.cer',
chain: 'cert.ca_bundle',
Expand Down
36 changes: 18 additions & 18 deletions test/test-examples-server-https-fail-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,76 +15,76 @@ describe("Alexa App Server with Examples & HTTPS fail checking", function() {

it("fails to mount due to missing HTTPS parameters", function() {
testServer = alexaAppServer.start({
port: 3000,
httpsPort: 3000,
server_root: 'invalid_examples',
https: true
httpsEnabled: true
});

expect(testServer.instance).to.not.exist;
expect(testServer.httpsInstance).to.not.exist;
});

it("fails to mount due to invalid private key and certificate", function() {
testServer = alexaAppServer.start({
port: 6000,
httpsPort: 6000,
server_root: 'invalid_examples',
https: true,
httpsEnabled: true,
privateKey: 'invalid-private-key.pem',
certificate: 'invalid-cert.cer'
});

expect(testServer.instance).to.not.exist;
expect(testServer.httpsInstance).to.not.exist;
});

it("fails to mount due to invalid CA chain file", function() {
testServer = alexaAppServer.start({
port: 6000,
httpsPort: 6000,
server_root: 'invalid_examples',
https: true,
httpsEnabled: true,
privateKey: 'private-key.pem',
certificate: 'cert.cer',
chain: 'invalid-cert.ca_bundle'
});

expect(testServer.instance).to.not.exist;
expect(testServer.httpsInstance).to.not.exist;
});

it("fails to mount due to no passphrase given", function() {
testServer = alexaAppServer.start({
port: 6000,
httpsPort: 6000,
server_root: 'invalid_examples',
https: true,
httpsEnabled: true,
privateKey: 'private-key.pem',
certificate: 'cert.cer',
chain: 'cert.ca_bundle'
});

expect(testServer.instance).to.not.exist;
expect(testServer.httpsInstance).to.not.exist;
});

it("fails to mount due to invalid passphrase", function() {
testServer = alexaAppServer.start({
port: 6000,
httpsPort: 6000,
server_root: 'invalid_examples',
https: true,
httpsEnabled: true,
privateKey: 'private-key.pem',
certificate: 'cert.cer',
chain: 'cert.ca_bundle',
passphrase: "test321"
});

expect(testServer.instance).to.not.exist;
expect(testServer.httpsInstance).to.not.exist;
});

it("fails to mount due to invalid port", function() {
testServer = alexaAppServer.start({
port: -1,
httpsPort: -1,
server_root: 'invalid_examples',
https: true,
httpsEnabled: true,
privateKey: 'private-key.pem',
certificate: 'cert.cer',
passphrase: "test123"
});

expect(testServer.instance).to.not.exist;
expect(testServer.httpsInstance).to.not.exist;
});
});
17 changes: 12 additions & 5 deletions test/test-examples-server-https-support-extended.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ describe("Alexa App Server with Examples & more HTTPS support", function() {

it("should have an HTTPS server instances running", function() {
testServer = alexaAppServer.start({
port: 3000,
httpsPort: 3000,
server_root: 'examples',
https: true,
httpsEnabled: true,
privateKey: 'private-key.pem',
certificate: 'cert.cer',
chain: 'cert.ca_bundle',
Expand All @@ -52,6 +52,7 @@ describe("Alexa App Server with Examples & more HTTPS support", function() {
.get('/alexa/helloworld')
.expect(200).then(function(response) {
expect(testServer.instance).to.exist;
expect(testServer.httpsInstance).to.exist;
return tcpPortUsed.check(3000).then(function(inUse) {
expect(inUse).to.equal(true);
});
Expand All @@ -71,18 +72,20 @@ describe("Alexa App Server with Examples & more HTTPS support", function() {
.get('/alexa/helloworld')
.expect(200).then(function(response) {
expect(testServer.instance).to.exist;
expect(testServer.httpsInstance).to.not.exist;
return tcpPortUsed.check(3000).then(function(inUse) {
expect(inUse).to.equal(true);
});
});
});

it("should have an HTTPS server instances running", function() {
it("should have both an HTTP and HTTPS server instances running", function() {
testServer = alexaAppServer.start({
port: 6000,
port: 3000,
httpsPort: 6000,
host: '127.0.0.1',
server_root: 'examples',
https: true,
httpsEnabled: true,
privateKey: 'private-key.pem',
certificate: 'cert.cer',
chain: 'cert.ca_bundle',
Expand All @@ -93,8 +96,12 @@ describe("Alexa App Server with Examples & more HTTPS support", function() {
.get('/alexa/helloworld')
.expect(200).then(function(response) {
expect(testServer.instance).to.exist;
expect(testServer.httpsInstance).to.exist;
return tcpPortUsed.check(6000).then(function(inUse) {
expect(inUse).to.equal(true);
return tcpPortUsed.check(3000).then(function(inUse) {
expect(inUse).to.equal(true);
});
});
});
});
Expand Down

0 comments on commit b828ccd

Please sign in to comment.