Thursday, December 26, 2013

HTTPS server (SSL) Implementation in node.js

Node.js provides HTTP server as one of the core libraries. So there is no need for a separate HTTP server. But if you are developing any application in node js which needs secure transactions then HTTPS server is necessary for it. So it can allow private information to be transmitted without the problems of eavesdropping, data tampering, or message forgery between your node.js  server and your visitor’s browser.
Developer should add SSL certificate in code to implement HTTPS server in node.js. In my last post I have explained, how to create self-signed SSL certificate in Ubuntu. It has commands and detail explanation for generating self-signed SSL in any Unix OS.  So for testing purpose you do not need to buy SSL certificate.
You can implement HTTPS server using simple steps:
Prerequisites:
  • node.js
Before start to implement HTTPS server please make sure that you have installed node.js in your system.
Commands to Create self-signed SSL certificate 
If you need a detail explanation and process, you can get it here. Following are commands to generate self-signed SSL in any Unix OS :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Become root first

$ apt-get install openssl

$ mkdir /etc/ssl/self-signed && cd /etc/ssl/self-signed

$ openssl genrsa -des3 -out server.key 2048

$ openssl rsa -in server.key -out server.key.insecure

$ mv server.key server.key.secure && mv server.key.insecure server.key

$ openssl req -new -key server.key -out server.csr

$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Above commands will generate two files server.key(private key to SSL certificate) and server.crt(signed certificate) in /etc/ssl/self-signed folder.
Node.js Code to implement HTTPS server
Now create a file server.js in your system. And add following code in it :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Include the https and ,file system modules
var https = require('https');
var fs = require('fs');

//Create the server options object, specifying the SSL key & cert
var options = {
key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
cert: fs.readFileSync('/etc/ssl/self-signed/server.crt')
};

//Create the HTTPS enabled server - listening on port 443
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(443);
Code Explanation :
Following is line by line explanation of above code:
1
2
var https = require('https');
var fs = require('fs');
In above two lines we are adding HTTPS and FS modules which are required for SSL implementation. After including require modules we need to add SSL files (key and cert files) path. We have already generated self-signed SSL and its files are stored in /etc/ssl/self-signed folder.
1
2
3
4
var options = {
key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
cert: fs.readFileSync('/etc/ssl/self-signed/server.crt')
};
In above code we are adding server.key and server.crt files path in options array variable. Now we need to create HTTPS server and pass this files through options variable to it.
1
2
3
4
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(443);
In above code lines, we have created HTTPS server by using createServer method. In it we have passed options variable as a parameter which contains SSL files path. And this server is listening on port no 443 which is a HTTPS port.
If you buy authenticated SSL certificate from any trusted service provider then you will get .key and .crt files. Just save this files in your system and replace self-signed SSL files path with this new authenticated files in above code.
Now open terminal and goto path where you have saved server.js file in your system. And run following command:
1
node server.js
Now open your favourite browser and enter https://localhost in address bar and press enter key. You will get hello world text in browser. That means you have successfully implemented HTTPS server in node.js. Please note that if you are using any other port than 443 then please mention that port number in url. For example, if you are using port number 8443 then enter https://localhost: 8443 url in browser.


No comments: