Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using https with express io

So I am new to express and io but I had a server running fine for webRTC but now there is a deprecated method in webRTC that only runs on https so I tried to create an https server but it starts and then immediately exits. I cannot figure out what is wrong and I do not get any errors. Also I am using an aws ec2 to run the express io server. Maybe someone can spot where in my syntax/implementation I am going wrong.

Note I have been googling around for the past half hour and cannot figure it out

Here is the code:

var connect = require('connect');
    var https = require('https');

    var fs = require('fs');
var express = require('express.io');
var app = express();
//app.http().io();
var PORT = 443;

var options = {
 key: fs.readFileSync('../server.key'),
 cert: fs.readFileSync('../server.crt')
};
app.https(options).io();
//var app = https.createServer(options, app1);

console.log('server started on port ' + PORT);

app.use(express.static(__dirname + '/public'));

app.get('/', function(req, res){
    res.render('index.ejs');
});

app.listen(PORT);

app.io.route('ready', function(req) {
      req.io.join(req.data.chat_room);
      req.io.join(req.data.signal_room);
      app.io.room(req.data).broadcast('announce', {
            message: 'New client in the ' + req.data + ' room.'
    })
})

Update

I am putting a bounty on this because I would like someone to provide me with a complete answer on setting up the server for production.

like image 948
sebenalern Avatar asked Apr 15 '16 06:04

sebenalern


People also ask

Does Express support HTTPS?

Enable HTTPS in Expressjs and your server should be available at address https://localhost:3000 . Please be aware that browsers reject self-signed certificates by default, so when you open https://localhost:3000 for the first time, you'll see a browser warning instead of an expected page.

Does socket IO use HTTPS?

If you want socket.io to use https, just pass a key param to the listen() function. You can also run your server behind stunnel.

How do I make HTTPS Express?

Go to the terminal and run the following command. After creation adds key & cert file in your code, and pass the options to the server. const express = require('express'); const https = require('https'); const fs = require('fs'); const port = 3000; var key = fs. readFileSync(__dirname + '/../certs/selfsigned.


1 Answers

You need to add a rule for port 443 in a Security Group for your instance.

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html might help.

like image 117
Dave Avatar answered Sep 29 '22 11:09

Dave