From d9651b7d0fe5fb59ccea5b2875a65498336af46f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5var=20Aamb=C3=B8=20Fosstveit?= Date: Mon, 22 Oct 2018 10:13:09 +0200 Subject: [PATCH] Cleanup of code, and redirect for http --- server/http-helpers.js | 50 ++++++++++++++++++++++++++---------------- server/server.js | 13 +++++++++++ 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/server/http-helpers.js b/server/http-helpers.js index de66801..9195b2f 100644 --- a/server/http-helpers.js +++ b/server/http-helpers.js @@ -1,31 +1,43 @@ 'use strict'; const headers = { - "access-control-allow-origin": "*", - "access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS", - "access-control-allow-headers": "content-type, accept", - "access-control-max-age": 10, - "Content-Type": "application/json" + 'access-control-allow-origin': '*', + 'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS', + 'access-control-allow-headers': 'content-type, accept', + 'access-control-max-age': 10, + 'Content-Type': 'application/json' }; -exports.prepareResponse = function(req, cb) { - var data = ""; - req.on('data', function(chunk) { data += chunk; }); - req.on('end', function() { cb(data); }); +exports.prepareResponse = (req, cb) => +{ + let data = ''; + + req.on('data', (chunk) => + { + data += chunk; + }); + + req.on('end', () => + { + cb(data); + }); }; -exports.respond = function(res, data, status) { - status = status || 200; - res.writeHead(status, headers); - res.end(data); +exports.respond = (res, data, status) => +{ + status = status || 200; + res.writeHead(status, headers); + res.end(data); }; -exports.send404 = function(res) { - exports.respond(res, 'Not Found', 404); +exports.send404 = (res) => +{ + exports.respond(res, 'Not Found', 404); }; -exports.redirector = function(res, loc, status) { - status = status || 302; - res.writeHead(status, { Location: loc }); - res.end(); +exports.redirector = (res, loc, status) => +{ + status = status || 302; + res.writeHead(status, { Location: loc }); + res.end(); }; diff --git a/server/server.js b/server/server.js index 36c95a5..9eba0ba 100755 --- a/server/server.js +++ b/server/server.js @@ -97,6 +97,19 @@ httpsServer.listen(config.listeningPort, '0.0.0.0', () => logger.info('Server running on port: ', config.listeningPort); }); +const httpServer = app.createServer(); + +// set up a route to redirect http to https +http.get('*', (req, res) => +{ + res.redirect('https://' + req.headers.host + req.url); +}) + +httpServer.listen(config.listeningRedirectPort, '0.0.0.0', () => +{ + logger.info('Server redirecting port: ', config.listeningRedirectPort); +}); + const io = require('socket.io')(httpsServer); // Handle connections from clients.