Skip to content
This repository was archived by the owner on Mar 10, 2024. It is now read-only.

Commit 66b230a

Browse files
authored
Merge pull request #24 from CodeWe-projet/http-redirect
Http redirect
2 parents ab9df58 + 8b00759 commit 66b230a

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/app.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,22 @@ const index = require('./routes/index');
1616
const editor = require('./routes/editor');
1717
const legal = require('./routes/legal');
1818
const config = require('./config/config');
19+
const ssl = config.SSL;
1920

2021
const app = express();
2122
app.disable("x-powered-by");
2223

24+
if (ssl) {
25+
app.use('*', function (req, res, next) {
26+
if (req.secure) {
27+
next();
28+
} else {
29+
const target = (req.headers.host.includes(':') ? req.headers.host.split(':')[0] : req.headers.host) + ':' + config.PORT;
30+
res.redirect('https://' + target + req.url);
31+
}
32+
});
33+
}
34+
2335
// Configure views folder
2436
nunjucks.configure(path.join(__dirname, 'views'), {
2537
autoescape: true,
@@ -51,13 +63,6 @@ app.use('/', index);
5163
app.use('/editor', editor);
5264
app.use('/legal', legal);
5365

54-
// redirect to https when ssl is active
55-
if (config.SSL) {
56-
app.get('*', function(req, res) {
57-
res.redirect('https://' + req.headers.host + req.url);
58-
});
59-
}
60-
6166
// 404 error
6267
app.all('*', (req, res) => {
6368
res.status(404).render('404.html', {production: config.PRODUCTION, client_versobe: config.CLIENT_VERBOSE});

src/config/config dist.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"SSL": false,
1111
"KEY_FILE_SSL": null,
1212
"CERT_FILE_SSL" : null,
13+
"REDIRECT_PORT": null,
1314
"METRICS": false,
1415
"METRICS_PORT": 8000
1516
}

src/server.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const host = configs.HOST;
1414
const port = configs.PORT;
1515
const DEBUG = configs.DEBUG;
1616
const ssl = configs.SSL;
17+
const metrics = configs.METRICS;
1718
const sslKeyPath = configs.KEY_FILE_SSL;
1819
const sslCertPath = configs.CERT_FILE_SSL;
1920

@@ -27,9 +28,11 @@ const config = require('./config/config');
2728

2829
const http = ssl ? require('https') : require('http');
2930
const server = http.createServer(options, app);
30-
if (configs.METRICS) {
31-
const {metricsApp} = require('./metricsApp');
32-
var metricsServer = require('http').createServer(metricsApp);
31+
32+
if (ssl && configs.REDIRECT_PORT !== null) {
33+
require('http').createServer(app).listen(configs.REDIRECT_PORT, host, () => {
34+
console.log(`http requests from ${configs.REDIRECT_PORT} are redirected to https on ${configs.PORT}`)
35+
})
3336
}
3437

3538
// config websockets
@@ -39,9 +42,11 @@ require('./socket/socket')(wss);
3942

4043

4144
server.listen(port, host, () => {
42-
console.log('Server Started!');
45+
console.log(`Server Started on ${host}:${port}`);
4346
});
4447

45-
if (config.METRICS) {
48+
if (metrics) {
49+
const {metricsApp} = require('./metricsApp');
50+
const metricsServer = require('http').createServer(metricsApp);
4651
metricsServer.listen(config.METRICS_PORT);
4752
}

0 commit comments

Comments
 (0)