Skip to content

Commit

Permalink
Adding https
Browse files Browse the repository at this point in the history
  • Loading branch information
richa008 committed Dec 14, 2019
1 parent e172478 commit 8eae661
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 41 deletions.
9 changes: 9 additions & 0 deletions ExpressPassport-TokenAuth/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ connect.then((db) => {

var app = express();

// Redirect http to https
app.all("*", (req, res, next) => {
if (req.secure) {
return next();
} else {
res.redirect(307, `https://${req.hostname}:${app.get("securePort")}${req.url}`);
}
})

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
Expand Down
11 changes: 11 additions & 0 deletions ExpressPassport-TokenAuth/bin/cert.csr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-----BEGIN CERTIFICATE REQUEST-----
MIIBlDCB/gIBADA7MQswCQYDVQQGEwJVUzELMAkGA1UECAwCV0ExEDAOBgNVBAcM
B1NlYXR0bGUxDTALBgNVBAMMBHRlc3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ
AoGBAJ6hgd0UyrrINIRkAJRKKhuuojwFgBOatU800li0+YUEwm8n8VBDP/I78Zxu
Jj5yOYdg0Zk/PHiAbZadTkkoFCnv8DedA1ePFtKsnBR0v+A/bQqumhL00+TYdLOj
QygSm1NdMCkcJ6PStsCS22vAiEmG9pgxRrZBlDcgLmuwQ5INAgMBAAGgGjAYBgkq
hkiG9w0BCQcxCwwJcmljaGExMjM0MA0GCSqGSIb3DQEBCwUAA4GBABQ2bQQkZAUV
RvFPgtWv4Av/EgEUeiDliwSdFeh0Gsfy3of6mMEHg7K94iaGuOoN2oMUyG/HUDPG
rF1QyvM22CSZiMAPgf/S5ADcW0vc4WDRX052NVW7N50u961RI3XGcpzWHqmesajd
dQtGV76IyzrWX0CV3gZrOtD1nVyw8haj
-----END CERTIFICATE REQUEST-----
13 changes: 13 additions & 0 deletions ExpressPassport-TokenAuth/bin/certificate.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-----BEGIN CERTIFICATE-----
MIIB7TCCAVYCCQDJANNgw1M2fzANBgkqhkiG9w0BAQUFADA7MQswCQYDVQQGEwJV
UzELMAkGA1UECAwCV0ExEDAOBgNVBAcMB1NlYXR0bGUxDTALBgNVBAMMBHRlc3Qw
HhcNMTkxMjE0MTgyMDQ0WhcNMjAwMTEzMTgyMDQ0WjA7MQswCQYDVQQGEwJVUzEL
MAkGA1UECAwCV0ExEDAOBgNVBAcMB1NlYXR0bGUxDTALBgNVBAMMBHRlc3QwgZ8w
DQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAJ6hgd0UyrrINIRkAJRKKhuuojwFgBOa
tU800li0+YUEwm8n8VBDP/I78ZxuJj5yOYdg0Zk/PHiAbZadTkkoFCnv8DedA1eP
FtKsnBR0v+A/bQqumhL00+TYdLOjQygSm1NdMCkcJ6PStsCS22vAiEmG9pgxRrZB
lDcgLmuwQ5INAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAnD5F3yCQTzQV19//Ffc3
+oVXWByHMFXFwAWzOUKo/KWqCTKmrtNf8db9/5SECSnuW16zwbTEKsIZxqc6ZuAe
E2DxNrWvsVR6XPiHDKKtdilgS9dxmCnQIweJ6MYwc8Jppf1eb+9LGeiohRFMjidZ
hOKTzUIEjR/Olt6GIYTlrFI=
-----END CERTIFICATE-----
15 changes: 15 additions & 0 deletions ExpressPassport-TokenAuth/bin/private.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCeoYHdFMq6yDSEZACUSiobrqI8BYATmrVPNNJYtPmFBMJvJ/FQ
Qz/yO/GcbiY+cjmHYNGZPzx4gG2WnU5JKBQp7/A3nQNXjxbSrJwUdL/gP20KrpoS
9NPk2HSzo0MoEptTXTApHCej0rbAkttrwIhJhvaYMUa2QZQ3IC5rsEOSDQIDAQAB
AoGAGzLsFmErRY1wW8bdxo9U2vw9evcid7oOHm+bJXSaV/Fx+ScrHCz7uOgITF8i
lZB2Rr0WEyWhk7PJloqQUsDZe/YrVWAm7viBqi8LkpJLSxhXwCmdBpX6JhJuNzfr
DjzNXmdAi8UxryKd7c+5e/Hjms2NWwhAatf7d8LpnP1uNgECQQDR4DNMvQlc6Zx1
4QimHGJkZlYBSXQyQ0nRaWI3FHTKf6sTmw6QUkUPcBWQ+jaok08E0BxqLgLzhguJ
RQt0L/mNAkEAwX45Ijm0S1vgSYIrIqN1wOYdvR8K0H2at9kL46T1H+JbM8IwQ2ZH
OOW+fmYUMCOHaSeteVTpJvRdPs443diagQJAduLNrHaYyyz6GDw1LgN+KOlfZrM5
Qw+izSEpJZJM5jiTrDN1eINh43DWhp12QN/RLyTEM9GEknYoKsYVyucyMQJBAKeK
jxgmlQnnnRapYoZUuYJ0/u0iMvACwnRMuPG9hQ69pjD0BWxm5GbM8BxscTCZoweX
dqsnrdawIwld8XmOs4ECQEybo4WKVWQ510Kr3Yw9MesuTLBmZXonkD+D0EX7Y0zg
bA+CM53Z4qVJWp9K75mhPJouCocgR7/Ae4yhg1QjvBw=
-----END RSA PRIVATE KEY-----
100 changes: 59 additions & 41 deletions ExpressPassport-TokenAuth/bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,44 @@
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('confusionserver:server');
var http = require('http');
const app = require('../app');
const debug = require('debug')('confusionserver:server');
const http = require('http');
const https = require('https');
const fs = require('fs');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
app.set('securePort', port + 443);

/**
* Create HTTP server.
*/

var server = http.createServer(app);
const server = http.createServer(app);

/**
* Create HTTPS server.
*/

const options = {
key: fs.readFileSync(__dirname + '/private.key'),
cert: fs.readFileSync(__dirname + '/certificate.pem')
}
const secureServer = https.createServer(options, app);

/**
* Listen on provided port, on all network interfaces.
*/
secureServer.listen(app.get('securePort'), () => {
console.log(`Server listening on port ${app.get('securePort')}`);
});
secureServer.on('error', onError);
secureServer.on('listening', onListening);

server.listen(port);
server.on('error', onError);
Expand All @@ -34,57 +52,57 @@ server.on('listening', onListening);
*/

function normalizePort(val) {
var port = parseInt(val, 10);
const port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}
if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}
if (port >= 0) {
// port number
return port;
}

return false;
return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
const addr = server.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}

0 comments on commit 8eae661

Please sign in to comment.