diff --git a/config/default.json b/config/default.json index dce6455..5d4d63b 100644 --- a/config/default.json +++ b/config/default.json @@ -1,7 +1,8 @@ { "Server": { "port": 4433, - "hostname": "https://shimapan.rocks" + "hostname": "https://shimapan.rocks", + "tls": true }, "Database": { "host": "mongodb://localhost:27017/shimapan" diff --git a/config/dev.json b/config/dev.json index 3af8a0a..ae2bf0c 100644 --- a/config/dev.json +++ b/config/dev.json @@ -1,7 +1,8 @@ { "Server": { "port": 8080, - "hostname": "http://localhost:8080" + "hostname": "http://localhost:8080", + "tls": false }, "Database": { "host": "mongodb://localhost:27017/shimapan-dev" diff --git a/config/test.json b/config/test.json index 26fb1e4..4f32322 100644 --- a/config/test.json +++ b/config/test.json @@ -1,7 +1,8 @@ { "Server": { "port": 8080, - "hostname": "http://localhost:8080" + "hostname": "http://localhost:8080", + "tls": false }, "Database": { "host": "mongodb://localhost:27017/shimapan-test" diff --git a/package.json b/package.json index 7ecfac5..5e3ee44 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,8 @@ "test": "npx mocha", "build": "npx gulp", "start": "npx gulp start", - "watch": "npx gulp watch" + "watch": "npx gulp watch", + "genkey": "openssl req -newkey rsa:2048 -nodes -keyout privkey.pem -x509 -days 3650 -out cert.pem" }, "repository": { "type": "git", diff --git a/server.js b/server.js index 624881a..7f5942b 100755 --- a/server.js +++ b/server.js @@ -1,3 +1,5 @@ +const https = require('https'); +const fs = require('fs'); const express = require('express'); const bodyParser = require('body-parser'); const methodOverride = require('method-override'); @@ -70,9 +72,19 @@ app.use((err, req, res, next) => { // Start app const port = config.get('Server.port'); -const server = app.listen(port, () => { - console.log('Listening on port ' + port + '...\n'); -}); + +let server; +if (config.get('Server.tls')) { + const options = { + cert: fs.readFileSync('./cert.pem'), + key: fs.readFileSync('./privkey.pem'), + }; + server = https.createServer(options, app).listen(port, () => + console.log(`Listening on port ${port}...`)); +} else { + server = app.listen(port, () => + console.log(`Listening on port ${port}...`)); +} // Expose app module.exports.app = app;