Skip to content

Commit 127d8ed

Browse files
committed
Added HTTPS support
1 parent 828db17 commit 127d8ed

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
| `session.secret` | Required | Private string for cookie |
4848
| `session.ttl` | `36` | Cookie TTL in hours |
4949
| | | |
50+
| `ssl` | Optional | Enables HTTPS mode if filled |
51+
| `ssl.key` | Required | Local path to private key |
52+
| `ssl.cert` | Required | Local path to certificate file |
53+
| | | |
5054
| `devMode` | `false` | If `true` controllers and models refreshing on every request |
5155
| `port` | `8081` | API listing port |
5256
| `ws_timeout` | `60` | WebSocket request waiting timeout in seconds |

index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,29 @@ const DB = require('./DB');
6161
});
6262
});
6363

64+
// Creating HTTPS server, if ssl enabled
65+
if(config.ssl) {
66+
const https = require('https');
67+
var server = https.createServer({
68+
key: fs.readFileSync(ROOT + '/' + config.ssl.key, 'utf8'),
69+
cert: fs.readFileSync(ROOT + '/' + config.ssl.cert, 'utf8')
70+
}, app);
71+
}
72+
6473
// Dispatching requests
6574
const dispatch = require('./dispatch');
6675
dispatch.db = MainDB;
67-
require('express-ws')(app);
76+
require('express-ws')(app, config.ssl?server:undefined);
6877
app.ws('/ws', (ws, req) => dispatch.ws(req, ws));
6978
app.all('*', (req, res) => dispatch.http(req, res));
7079

80+
// Listening port
7181
config.port = config.port || 8081;
72-
app.listen(config.port, () => {
73-
console.log('[Core] Started at port ' + config.port);
74-
});
82+
const listenArgs = [
83+
config.port,
84+
() => console.log('[Core] Started' + (config.ssl?' with SSL':'') + ' at port ' + config.port)
85+
];
86+
87+
if(config.ssl) server.listen(...listenArgs);
88+
else app.listen(...listenArgs);
7589
})();

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dc-api-core",
3-
"version": "0.1.6",
3+
"version": "0.1.7",
44
"author": "DimaCrafter",
55
"homepage": "https://github.com/DimaCrafter/dc-api-core",
66
"bugs": "https://github.com/DimaCrafter/dc-api-core/issues",
@@ -10,7 +10,7 @@
1010
"express": "^4.16.4",
1111
"express-session": "^1.15.6",
1212
"express-ws": "^4.0.0",
13-
"mongoose": "^5.3.4",
13+
"mongoose": "^5.3.7",
1414
"mongoose-auto-increment": "^5.0.1"
1515
}
1616
}

0 commit comments

Comments
 (0)