Wednesday, October 08, 2014

Create HTTPS over Express Node.js Server On LocalHost With OpenSSL

https connection was thought as More secured but After Heart Bleed.Probably http is more secure :p .And Recently you would heard that Google Prefers HTTPS connection for SEO boost up and Higher ranks on Google Search.Many guys switched over Https connections.However This is a Simple security for transferring of data with encrypted format.However both the Side have Public and Private Key to ensure safe transfer of data and Disallowing Hackers/Anonymous peoples to Access your data.



Reference : Download Demo 

Installation :

Before starting this module, i would like to go through some of My Last post on basic articles in node.js
Note : Here we are simply Creating Self Signed Certificate to create HTTPS connection.So,Browsers may Warn hackers could steal/compromise your Data.This is for Development purpose.For production purpose Buy SSL certificate from Godaddy or leading SSL providers and Set up in your server.


Procedure to generate SSL certificate:

1) Generate RSA key with Openssl

C:\> openssl genrsa 1024 > /path/to/key.pem
2) Execute the Below Command to Generate certificate SSL with your self signature.with passphrase

C:\> openssl req -new -key /path/to/key.pem -out csr.pem
C:\> openssl x509 -req -days 365 -in key.pem -signkey /path/to/file.pem -out /path/to/ssl.crt

Take these two files to your server directory (node project directory)

Server.js :

Set up connection with including https module and make it to listen to a port namely 443 for Secured connections.
with the help of fs module just provide the path of key.pem and ssl.crt,passphrase(optional)or else you will be requested in commandline when starting your server each time.



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

    app.get('/', function (req, res) {
    res.send("hello");
    });

https.createServer({
      key: fs.readFileSync('key.pem'),
      cert: fs.readFileSync('ssl.pem')
    }, app).listen(443);


Now issue npm server and start your server.now you will be prompted for passphrase and if it is correct your server is ready to take off!

now point to the browser and https://localhost:443/ will show some Error/Security message.simply accept it and test your server.note this happens because of creating self signed certificate.while going into production mode buy a commercial and register with certified SSL verifier.

Have a view at my secured https connection in localhost! with some message.just click advanced and proceed you may get your normal route.


Create HTTPS over Express Node.js Server On LocalHost With OpenSSL



Note : Here we are simply Creating Self Signed Certificate to create HTTPS connection.So,Browsers may Warn hackers could steal/compromise your Data.This is for Development purpose.For production purpose Buy SSL certificate from Godaddy or leading SSL providers and Set up in your server.


For Errors/bugs/suggestions ideas just comment below or mail me s.shivasurya@gmail.com or connect with me in Facebook/Google+/Twitter for more details.Share is care.

1 comments:

Post a Comment

feel free to post your comments! Don't Spam here!