Skip to content

Instantly share code, notes, and snippets.

@alexandrevilain
Last active August 16, 2016 09:31
Show Gist options
  • Save alexandrevilain/9822a1f8a1fdfe28e3a117fce531f1ca to your computer and use it in GitHub Desktop.
Save alexandrevilain/9822a1f8a1fdfe28e3a117fce531f1ca to your computer and use it in GitHub Desktop.
How to add SSL support for express generator - With self signed certificates for dev
  1. Generate your express app with express generator : express --git --hbs .

  2. Create genSSL.sh : touch genSSL.sh

  3. Launch genSSL.sh:

chmod +x genSSL.sh
./genSSL.sh
  1. Edit bin/www file with www file given in this gist.

  2. Thanks : https://matoski.com/article/node-express-generate-ssl/

#!/bin/bash
cd bin
mkdir ssl
cd ssl
openssl genrsa -des3 -out ca.key 1024
openssl req -new -key ca.key -out ca.csr
openssl x509 -req -days 365 -in ca.csr -out ca.crt -signkey ca.key
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
cp server.key server.key.passphrase
openssl rsa -in server.key.passphrase -out server.key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('test:server');
var http = require('http');
var https = require('https');
var fs = require('fs');
var path = require("path");
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
var options = {
key: fs.readFileSync(path.join(__dirname, 'ssl', 'server.key')),
cert: fs.readFileSync(path.join(__dirname, 'ssl', 'server.crt')),
ca: fs.readFileSync(path.join(__dirname, 'ssl', 'ca.crt')),
requestCert: true,
rejectUnauthorized: false
}
var secureServer = https.createServer(options, app)
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
secureServer.listen(port +1);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
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;
}
}
/**
* 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment