Skip to content

Commit 0bae598

Browse files
author
surendra_vidiyala
committed
created private key and certificate using openssl
1 parent f09c99c commit 0bae598

5 files changed

Lines changed: 194 additions & 8 deletions

File tree

‎README.md‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,146 @@
11
# https-secure-server
2+
* Exploring the use of the HTTPS server core node module to create and run a secure server and generating your private key and public certificate and use them to configure the Node HTTPS server.
3+
- Configure a secure server in Node using the core HTTPS module
4+
- Generate the private key and public certificate and configure the HTTPS server
5+
- Redirect traffic from the insecure HTTP server to a secure HTTPS server.
6+
# Updating the bin/www File
7+
- Open the www file in the bin directory and update its contents as follows:
8+
9+
````
10+
#!/usr/bin/env node
11+
/**
12+
* Module dependencies.
13+
*/
14+
15+
var app = require('../app');
16+
var debug = require('debug')('rest-server:server');
17+
var http = require('http');
18+
var https = require('https');
19+
var fs = require('fs');
20+
21+
/**
22+
* Get port from environment and store in Express.
23+
*/
24+
25+
var port = normalizePort(process.env.PORT || '3000');
26+
27+
app.set('port', port);
28+
app.set('secPort',port+443);
29+
30+
/**
31+
* Create HTTP server.
32+
*/
33+
34+
var server = http.createServer(app);
35+
36+
/**
37+
* Listen on provided port, on all network interfaces.
38+
*/
39+
40+
server.listen(port, function() {
41+
console.log('Server listening on port ',port);
42+
});
43+
server.on('error', onError);
44+
server.on('listening', onListening);
45+
46+
/**
47+
* Create HTTPS server.
48+
*/ var options = {
49+
key: fs.readFileSync(__dirname+'/private.key'),
50+
cert: fs.readFileSync(__dirname+'/certificate.pem')
51+
};
52+
53+
var secureServer = https.createServer(options,app);
54+
55+
/**
56+
* Listen on provided port, on all network interfaces.
57+
*/
58+
59+
secureServer.listen(app.get('secPort'), function() {
60+
console.log('Server listening on port ',app.get('secPort'));
61+
});
62+
secureServer.on('error', onError);
63+
secureServer.on('listening', onListening);
64+
65+
/**
66+
* Normalize a port into a number, string, or false.
67+
*/
68+
69+
function normalizePort(val) {
70+
var port = parseInt(val, 10);
71+
if (isNaN(port)) {
72+
// named pipe
73+
return val;
74+
}
75+
if (port >= 0) {
76+
// port number
77+
return port;
78+
}
79+
return false;
80+
}
81+
82+
/**
83+
* Event listener for HTTP server "error" event.
84+
*/
85+
86+
function onError(error) {
87+
if (error.syscall !== 'listen') {
88+
throw error;
89+
}
90+
var bind = typeof port === 'string'
91+
? 'Pipe ' + port
92+
: 'Port ' + port;
93+
94+
// handle specific listen errors with friendly messages
95+
switch (error.code) {
96+
case 'EACCES':
97+
console.error(bind + ' requires elevated privileges');
98+
process.exit(1);
99+
break;
100+
101+
case 'EADDRINUSE':
102+
console.error(bind + ' is already in use');
103+
process.exit(1);
104+
break;
105+
106+
default:
107+
throw error;
108+
}
109+
}
110+
111+
/**
112+
* Event listener for HTTP server "listening" event.
113+
*/
114+
115+
function onListening() {
116+
var addr = server.address();
117+
var bind = typeof addr === 'string'
118+
? 'pipe ' + addr
119+
: 'port ' + addr.port;
120+
debug('Listening on ' + bind);
121+
}
122+
````
123+
124+
- Updating app.js
125+
- Open app.js and add the following code to the file:
126+
127+
````
128+
// Secure traffic only
129+
app.all('*', function(req, res, next){
130+
console.log('req start: ',req.secure, req.hostname, req.url, app.get('port'
131+
      ));
132+
if (req.secure) {
133+
return next();
134+
};
135+
136+
res.redirect('https://'+req.hostname+':'+app.get('secPort')+req.url);
137+
});
138+
````
139+
# Generating Private Key and Certificate
140+
* Go to the bin folder and then create the private key and certificate by typing the following at the prompt:
141+
````
142+
openssl genrsa 1024 > private.key
143+
openssl req -new -key private.key -out cert.csr
144+
openssl x509 -req -in cert.csr -signkey private.key -out certificate.pem
145+
````
146+
Run the server and test.

‎app.js‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ var promoRouter = require('./routes/promoRouter');
2525
var leaderRouter = require('./routes/leaderRouter');
2626

2727
var app = express();
28+
// Secure traffic only
29+
app.all('*', function(req, res, next) {
30+
console.log('req start: ', req.secure, req.hostname, req.url, app.get('port'));
31+
if (req.secure) {
32+
return next();
33+
};
34+
35+
res.redirect('https://' + req.hostname + ':' + app.get('secPort') + req.url);
36+
});
2837

2938
// view engine setup
3039
app.set('views', path.join(__dirname, 'views'));
@@ -81,14 +90,6 @@ app.use(function(err, req, res, next) {
8190
error: {}
8291
});
8392
});
84-
// Secure traffic only
85-
app.all('*', function(req, res, next) {
86-
console.log('req start: ', req.secure, req.hostname, req.url, app.get('port'));
87-
if (req.secure) {
88-
return next();
89-
};
9093

91-
res.redirect('https://' + req.hostname + ':' + app.get('secPort') + req.url);
92-
});
9394

9495
module.exports = app;

‎bin/cert.csr‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-----BEGIN CERTIFICATE REQUEST-----
2+
MIIBnDCCAQUCAQAwXDELMAkGA1UEBhMCVVMxETAPBgNVBAgMCGlsbGlub2lzMRcw
3+
FQYDVQQHDA5Vbml2ZXJzaXR5UGFyazEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0
4+
cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDXFApmaRPL6IuT
5+
dyEKUOyj7rNis1oAn+Zixm+RvaPjUkhVLZq6rQYEfimnT71P8h64/IBwPDt4sSTq
6+
i6JnN+sJ3HLWvT8wlnTyVaxGDleWLC0P//lmj6YbRDMFTwADHn7/yAgXodvjQe5X
7+
7FwfSWWqWzJ8WskJaRg+2w6OixbBgwIDAQABoAAwDQYJKoZIhvcNAQELBQADgYEA
8+
IrGj6I29zlbkwqnBNtmE6qVv4SFaO9r23UVrhBMCN5NFX2a57OBMKO5e3SKGpbEK
9+
q1lnVQJJPAkbb394NDGsxBAi4ShDjssXSYnC6MCopj5TARkbbmR99yKHXEd1MV5U
10+
eCmkFqP2qfdTspR8pooWhf4I4XJFX4eIUYNeqiK6oxk=
11+
-----END CERTIFICATE REQUEST-----

‎bin/certificate.pem‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIICLzCCAZgCCQDjrInKRqVZpDANBgkqhkiG9w0BAQsFADBcMQswCQYDVQQGEwJV
3+
UzERMA8GA1UECAwIaWxsaW5vaXMxFzAVBgNVBAcMDlVuaXZlcnNpdHlQYXJrMSEw
4+
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwHhcNMTcwNjA4MjMxMTQz
5+
WhcNMTcwNzA4MjMxMTQzWjBcMQswCQYDVQQGEwJVUzERMA8GA1UECAwIaWxsaW5v
6+
aXMxFzAVBgNVBAcMDlVuaXZlcnNpdHlQYXJrMSEwHwYDVQQKDBhJbnRlcm5ldCBX
7+
aWRnaXRzIFB0eSBMdGQwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANcUCmZp
8+
E8voi5N3IQpQ7KPus2KzWgCf5mLGb5G9o+NSSFUtmrqtBgR+KadPvU/yHrj8gHA8
9+
O3ixJOqLomc36wnccta9PzCWdPJVrEYOV5YsLQ//+WaPphtEMwVPAAMefv/ICBeh
10+
2+NB7lfsXB9JZapbMnxayQlpGD7bDo6LFsGDAgMBAAEwDQYJKoZIhvcNAQELBQAD
11+
gYEAVICUKivP4HODvSd9xuVnz+XyigMC8NKi9GQdrUgs1FCZNXsMGJwW7AK0jhRR
12+
dbhftifyLvtS7OkQ4FyryGUNM/7ag/h2hUP+iTQLp61y1etffdD2EPIaR+zBlXrq
13+
IdMInMM9gyBAt36CDRrgV/OoB+fuKBs48NLF457Qwg4XT9s=
14+
-----END CERTIFICATE-----

‎bin/private.key‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIICWwIBAAKBgQDXFApmaRPL6IuTdyEKUOyj7rNis1oAn+Zixm+RvaPjUkhVLZq6
3+
rQYEfimnT71P8h64/IBwPDt4sSTqi6JnN+sJ3HLWvT8wlnTyVaxGDleWLC0P//lm
4+
j6YbRDMFTwADHn7/yAgXodvjQe5X7FwfSWWqWzJ8WskJaRg+2w6OixbBgwIDAQAB
5+
AoGAPc/5qTMbyGed7ZS4PzHAwOByV7R58mICIXMsT3N0LXnRZgGETZA6gYIM0JP/
6+
MYHBSKLw+tCkuuwWUv4sf4O3nYfwWzmXblqpvgYzKxYSdD7qioOpl/EpcSpkXNmM
7+
IAAE3dZ8KdKdKnE4MgqUGdnfUsCj6MBx0YPEQ60d4XAYJuECQQDzm91z3qQEdIbx
8+
hq9d1OGeNVN85DloqOek6EMoklQpHqbIrkeK0NsIoZLZ0gD17aJH34THMacFzgMX
9+
A+QldqGTAkEA4gSquip36N1wTHC0xz3+rcL7J0vf9GU95lsemimKbAirX0EEcsYU
10+
oLlkKqK9pgXI397Vib5B12E8+95uPYkWUQJADjyPppPeVfxRXpAWc49bWBmZtXTI
11+
/hrQ3sxMEHTswjJuYM/+klg+VLTMljo1oCmxEC453kJClo5Bk/jAPog+TQJASgs5
12+
Wy/ahKE2zvQFZVh7/LTpzp/eyJP6SWMEjYCaZnYYnmYQbQFTdKwHkqgp9R/kBONd
13+
lXiM3WBvuBvQhbxJYQJAer6sJUTrjRL901ZQBlPSFof77DkxBftmcOZKKZfikz1y
14+
wvDm/8Yp6oW8pCS5ImF4rrL0tKGKDWMlhGse734nbw==
15+
-----END RSA PRIVATE KEY-----

0 commit comments

Comments
 (0)