Sunday, November 10, 2013

Need to access your node.js app via HTTPS?

Recently I used several Javascript libraries/frameworks to develop some tools to boost my performance of setup environment and testing our product. Also it helps me to sharp my Javascript skills. I feel like my Javascript skill is catching up with my Java skill. And most importantly, I feel like writing Javascript is much more fun than writing Java.

Here is a simple note on how to serving content via node.js with HTTPS and make browser accept the certificate automatically.

First, use this commend from this The Most Common OpenSSL Commands to create your private key and certificate:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt

Second, install the certificate.crt to Keychain in Mac (You may need other solution for other OS). Follow the instructions in this Google Chrome, Mac OS X and Self-Signed SSL Certificates.

Last, I'm using the Connect middleware lib for node.js. To serve via HTTPS, you need something like:
var connect = require('connect'),
    http = require('http'),
    https = require('https'),
    fs = require('fs');

var app = connect().
  // ...
  // configure app accordingly

var options = {
  key: fs.readFileSync('privateKey.key'),
  cert: fs.readFileSync('certificate.crt')
};
https.createServer(options, app).listen(9999);

That's it. Once you start your server via node, you should be able to access your content via HTTPS.

1 comment: