Https Server Express ?

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. Https server express example is as :

 * Express version of server.js

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

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

var app = express.createServer(options);

app.get('/', function(req, res) {
    req.client.authorized ? 
        res.json({"status":"approved"}) : 
        res.json({"status":"denied"}, 401);

My Name is Dileep Singh, Noder, Javascript Lover & NoSql Developer, Fitness Freak, Love to travel new places, learning photography & Music Manic ♯ ♩ ♬

Posted in Node.Js Tagged with: