I am Hack Sparrow
Captain of the Internets.

Express.js HTTPS

How to set up a HTTPS Express.js Server

Setting up a HTTPS Express.js server is pretty easy and straightforward. First you need to make sure you have a signed SSL certificate for your server. In case you don't have one, you can create one for yourself this way:

$ openssl genrsa -out hacksparrow-key.pem 1024 
$ openssl req -new -key hacksparrow-key.pem -out certrequest.csr
$ openssl x509 -req -in certrequest.csr -signkey hacksparrow-key.pem -out hacksparrow-cert.pem

That creates a self-signed SSL certificate. Good for testing locally, but not recommended on a production server. Get a SSL certificate for your website from a reputed Certificate Authority.

The next step is similar to calling the regular createServer() method of HTTP server. Except, in this case we load the key and certificate and pass them to the createServer() methods in an object:

var fs = require('fs');

var hskey = fs.readFileSync('hacksparrow-key.pem');
var hscert = fs.readFileSync('hacksparrow-cert.pem')

var options = {
key: hskey,
cert: hscert
};
var app = require('express').createServer(options);

Once you have the HTTPS configuration in place:

$ node app

There you have your HTTPS Express.js server running!

4 Responses to “Express.js HTTPS”

  1. bcs says:

    Hello
    How do you create your own signed SSL certificate for HTTPS Express.js server on Windows?

    thanks
    Ben

  2. bcs says:

    can you please show how to create a signed SSL certificate on a windows server?
    Thanks

  3. Surendar says:

    Hi bcs I found answer for your question.You can create your own SSL certificate in windows too..Simply you need openssl.exe , it was available in git bash,cygwin etc… install git bash in windows and type below mentioned command in git bash…and enjoy!!!!!

    $ openssl genrsa -out hacksparrow-key.pem 1024
    $ openssl req -new -key hacksparrow-key.pem -out certrequest.csr
    $ openssl x509 -req -in certrequest.csr -signkey hacksparrow-key.pem -out hacksparrow-cert.pem

  4. Nick Baki says:

    1- Create your key and cert on linux
    2- Copy key.pem and cert.pem to your windows machine.

Make a Comment