Launching an SSL (HTTPS) Server in Node.js

JavaScript:

var https      = require("https");
var fs         = require("fs");
var key_file   = "/path/to/file.pem";
var cert_file  = "/path/to/file.crt";
var passphrase = "this is optional";
var config     = {
  key: fs.readFileSync(key_file),
 cert: fs.readFileSync(cert_file)
};
if(passphrase) {
  config.passphrase = passphrase;
}

https.createServer(config,app).listen(443);

CoffeeScript:

https      = require "https"
fs         = require "fs"
key_file   = "/path/to/file.pem"
cert_file  = "/path/to/file.crt"
passphrase = "this is optional"
config     = {
  key:  fs.readFileSync(key_file)
  cert: fs.readFileSync(cert_file)
}
config.passphrase = passphrase if passphrase?

https.createServer(config,app).listen(443)

Where /path/to/file.pem is the path to a file containing an RSA key, generated (for example) by:

openssl genrsa 1024 > /path/to/file.pem

and /path/to/file.crt is the path to a file containing an SSL certificate, generated (for example) by:

openssl req -new -key /path/to/file.pem -out csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey /path/to/file.pem -out /path/to/file.crt
Published 13 Mar 2014

 

This page was generated at 4:16 PM on 26 Feb 2018.
Copyright © 1999 - 2018 Rodney Waldhoff.