Abhiz
Abhiz

Reputation: 1030

Creating an HTTPs server with Node.js and Express

                   var https = require('https'),
        fs = require('fs'),
        express = require('express'),
        app = express();
        // cookieParser = require('cookie-parser'),
        // path = require('path'),
        // bodyParser = require('body-parser'),
        // https = require('http');


    var key = fs.readFileSync('encryption/star_massifsolutions_com.key');
    var cert = fs.readFileSync('encryption/massif_wildcard.crt');
    var ca = fs.readFileSync('encryption/DigiCertCA.crt');

    var httpsOptions = {
        key: key,
        cert: cert,
        ca: ca
    };


    https.createServer(httpsOptions, app).listen(8000, function () {
        console.log("server running at https://IP_ADDRESS:8000/")
    });
    app.get('/', function (req, res) {
        res.header('Content-type', 'text/html');
        return res.end('Hello World!');
    });

    // app.set('view', __dirname + '/views');
    // app.use(bodyParser.urlencoded({
    //     extended: true
    // }));
    // app.use(bodyParser.json({
    //     limit: '500mb'
    // }));


    // app.use('/', express.static(path.join(__dirname, '/dist/basic-structure')));

    // app.get('/**', function (req, res, next) {
    //     console.log(req, res, next);
    //     res.sendFile('index.html', {
    //         root: __dirname + '/dist/basic-structure'
    //     });
    // });

    console.log("listening to port 8000");

Here i have written hello world just to test my code.so in this case code runs but its not secure. I want my connection to be secure.In this case it shows deprecated http and shows certificate error.but and run unsecurly.

Again if I replace the hello world part with the commented part as shown in my code it doesn't even run with the deprecated http.if i replace the https with http it runs. I want help in running my edited code. If i am missing some points please let me know.

In short this code is running insecurly , i want to make it secure

Upvotes: 1

Views: 8673

Answers (2)

benfr
benfr

Reputation: 66

Not sure if I understand well, but if by "the code is not running" you mean your app, then it seems your 2nd code set simply try to run a server but not your app

To my understanding, you are defining your app as express but you are not using it, so it will not be delivered

So my guess is that you will need to use the https server command with the app and its options to link everything together (https and app) as suggested by @lx1412

I would try this :

var express = require('express'),
cookieParser = require('cookie-parser'),
path = require('path'),
bodyParser = require('body-parser'),
// https = require('http'),
https = require('https'),
app = express(),
fs = require('fs');


var key = fs.readFileSync('encryption/star_massifsolutions_com.key');
var cert = fs.readFileSync( 'encryption/massif_wildcard.crt' );
var ca = fs.readFileSync( 'encryption/DigiCertCA.crt' );

var httpsOptions = {
key: key,
cert: cert,
ca: ca
};

app.set('view',__dirname+'/views');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json({limit: '500mb'}));


app.use('/', express.static(path.join(__dirname,'/dist/basic-structure')));

app.get('/**', function(req, res, next) { 
    console.log(req, res, next); 
    res.sendFile('index.html', { root: __dirname + 
'/dist/basic-structure' }); 
});


// https.createServer(httpsOptions, (req, res) => {
// console.log("code works");
// res.writeHead(200);
// res.end('hello world\n');
// }).listen(8000);

https.createServer(httpsOptions, app).listen(8000, function () {
    console.log("code works");
    res.writeHead(200);
    res.end('hello world\n');
});

EDIT :

Can you simply try this and see how it behaves ? Also, can you provide your deprecated http and certificate error ?

app.get('/', function (req, res) {
   res.send('Hello World!');
});
https.createServer(httpsOptions, app).listen(8000, function () {
   console.log("server running at https://IP_ADDRESS:8000/")
});

Upvotes: 2

lx1412
lx1412

Reputation: 1200

It's simple.

var express = require('express'),
    cookieParser = require('cookie-parser'),
    path = require('path'),
    bodyParser = require('body-parser'),
    http = require('http'),
    app = express();

app.set('view',__dirname+'/views');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json({limit: '500mb'}));


app.use('/', express.static(path.join(__dirname,'/dist/basic-structure')));

app.get('/**', function(req, res, next) { 
    console.log(req, res, next); 
    res.sendFile('index.html', { root: __dirname + 
'/dist/basic-structure' }); 
});

//Start Server
//app.listen(3004, function(){
//    console.log('>>>3004')
//});
//complete your code here
https.createServer(httpsOptions,app).listen(8000);

Upvotes: -1

Related Questions