0

This is the code I used to connect http server.

var app = require('http').createServer(require('express')),
io = require('socket.io').listen(app),
util = require('util'),
connectionsArray = [], // maintain active connected client details
connectionStatistics = {'summary': {'instance_count': 0, 'user_count': 0, 'customer_count': 0}, 'customers': {}}, // for debugging purpose

server_port = 3000, // port on which nodejs engine to run
POLLING_INTERVAL = 10 * 1000, // 10 sec
pollingTimer = [], // timeouts for connected sockets
fs = require('fs'), // lib for file related operations
log_file = {
    'error': fs.createWriteStream(__dirname + '/debug.log', {flags: 'a'}), // file to log error messages
    'info': fs.createWriteStream(__dirname + '/info.log', {flags: 'a'}) // file to log info messages
};




var server = app.listen(server_port, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log('Please use your browser to navigate to http://%s:%s', host, port);
});

I want to include https connection in the above code. I tried to connect https using SSLCertificateFile and SSLCertificateKeyFile.

But it didn't work for me.

Shamnad
  • 35
  • 5

2 Answers2

0
  1. install https module ( yarn add https / npm i https )
  2. change options (ssl file path) as below :
    const options = {

            key: fs.readFileSync('./ssl/private.key'),
            cert: fs.readFileSync('./ssl/certificate.crt'),
            ca:fs.readFileSync('./ssl/ca_bundle.crt')
    }
    https.createServer(options, app).listen(port);
wu kuoming
  • 124
  • 1
  • 4
0

Try this snippet using express and https module instead of http

let fs = require('fs');

let https = require('https');

let express = require('express');

let app = express();

let options = { key: fs.readFileSync('./file.pem'), cert: fs.readFileSync('./file.crt') };

let serverPort = 3000;

let server = https.createServer(options, app);

let io = require('socket.io')(server);

io.on('connection', function(socket) { console.log('new connection'); });

server.listen(serverPort, function() { console.log('server up and running at %s port', serverPort); });

Arpit Bhatia
  • 173
  • 1
  • 8