1
                   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

4
  • what error are you getting ? Commented Oct 25, 2018 at 8:17
  • isn't that with ssl you also need the port in your httpsOptions. something like this. var httpsOptions = { host : 'localhost', port : 443, path : '/any/path', method : 'GET', headers : { 'Accept' : 'application/json' }, key: key, cert: cert, ca: ca }; Commented Oct 25, 2018 at 8:18
  • my terminal just got hanged. its running infinitely. not showing any error. @MukeshSharma Commented Oct 25, 2018 at 8:41
  • @KhajaMohammed I am not getting you why should i include all these. Commented Oct 25, 2018 at 8:44

2 Answers 2

2

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/")
});
Sign up to request clarification or add additional context in comments.

6 Comments

i have edited the questions a bit, if its not clear to you you can go through it again, please
yes i did this.and finally its was due to the certificate error. Finally i found a way
Glad to hear it ! May I ask what the certification error was so you found your way ?
Actually still i am not sure why this certificate error is there. I am not able to find out the reason. its saying 'Connection is not secure' ..Is it something missing from my part or its because of the backend developer? help me out i
Hard to tell, but if you have a big red warning message saying it is not secured, it can actually means your server/app is well delivered in HTTPS but it is an issue with the certificate itself (like date/expiration).. Also you can have clues if you have the ability to bypass the browser security (if it is certificate-related)
|
-1

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);

1 Comment

the ssl certificates are not included in your answer . I want to know why the 2nd set of code with the ssl certificates is running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.