Skip to content

Commit

Permalink
[new] add ssl support by adding certificate and privateKey as option
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Mollweide committed Sep 20, 2016
1 parent 8c35853 commit 92c3eb0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
25 changes: 19 additions & 6 deletions controller/AppController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

var express = require('express'),
https = require('https'),
app = express(),
bodyParser = require('body-parser'),
log = require('chip')(),
Expand Down Expand Up @@ -47,13 +48,21 @@ AppController.prototype = extend(AppController.prototype, {
* @param {object} options
* @param {string} options.dirName
* @param {object} options.swaggerImport
* @param {string} options.privateKey
* @param {string} options.certificate
* @param {string|undefined} options.jsVersion
* @public
*/
init: function (options) {

options = extend(this._defaults, options || {});

var logFunc = function () {
if (process.env.NODE_ENV !== 'test') {
log.info('server started at port ' + options.port);
}
};

this.options = options;
this.app = app;

Expand All @@ -66,19 +75,23 @@ AppController.prototype = extend(AppController.prototype, {
this.options.swaggerImport.dirName = this.options.dirName;
}

app.listen(options.port, function () {
if (process.env.NODE_ENV !== 'test') {
log.info('server started at port ' + options.port);
}
});

app.use('/src', express.static(__dirname + '/../src'));
app.use('/node_modules', express.static(__dirname + '/../node_modules'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.set('views', __dirname + '/../views');

if (this.options.privateKey && this.options.certificate) {
https.createServer({
key: this.options.privateKey,
cert: this.options.certificate
}, app).listen(this.options.port, logFunc);

return;
}

app.listen(this.options.port, logFunc);
}

});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-mock-server",
"version": "0.8.2",
"version": "0.9.0",
"description": "File based Node API mock server",
"email": "[email protected]",
"author": "Simon Mollweide <[email protected]>",
Expand Down
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ Default value: `3001`

A number value that is used to define the port.

#### options.privateKey
Type: `String`

A string value that is used to define the private key for ssl.

#### options.certificate
Type: `String`

A string value that is used to define the certificate for ssl.

#### options.funcPath
Type: `String|Array`
Optional
Expand Down

0 comments on commit 92c3eb0

Please sign in to comment.