NodeJS SSL Basic

Linux

This is Ubuntu example(Linux Ubuntu 14.04)
Install nodejs and openssl

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get update
sudo apt-get install nodejs

OpenSSL

Create self-certificate(CA)

openssl genrsa -des3 -out ca.key 1024
openssl req -new -key ca.key -out ca.csr
openssl x509 -req -days 3650 -in ca.csr -out ca.crt -signkey ca.key

You can get 10years ca.csr, ca.key, ca.crt.

(If you want to use authorized CA to create crt file, send ca.csr)

Create Server crt

openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

Finally wee need following files

  • ca.crt
  • server.crt
  • server.crt

Basic nodejs SSL

Keep files in same directory of js

main.js

var https = require('https');
var fs = require('fs');

var options = {
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.crt'),
  ca: fs.readFileSync('ca.crt'),
  requestCert: true,
  rejectUnauthorized: false
};

https.createServer(options, function(req, res) {
    res.setHeader('Conent-Type', 'text/plain');
    res.end('Hello World!\n');
}).listen(1337);

Start application

nodejs main.js

Ask key password please input
And please access https://localhost:1337

NodeJS SSL