|
1 | 1 | # 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. |
0 commit comments