How to use https with letsencrypt, socket.io and n

2019-03-01 05:22发布

Im trying to get https to work with socket.io and node.js. I can connect and all works if I test with a http page.

I get this error when I try to load the socket.io.js file.

GET https://my ip number:3000/socket.io/socket.io.js net::ERR_INSECURE_RESPONSE

Im linking like this

 <script src="https://my ip number:3000/socket.io/socket.io.js"></script>

And my server.js file looks like this

    var fs = require('fs');
    var https = require('https');
    var http = require('https');
    var express = require('express');
    var app = express();

    var options = {  
        key: fs.readFileSync('C:/Users/kund-admin/AppData/Roaming/letsencrypt-win-simple/httpsacme-v01.api.letsencrypt.org/manmade.se-key.pem'),
        cert: fs.readFileSync('C:/Users/kund-admin/AppData/Roaming/letsencrypt-win-simple/httpsacme-v01.api.letsencrypt.org/manmade.se-chain.pem')
    };

    var serverPort = 3000;
    var server = https.createServer(options, app);
    var io = require('socket.io').listen(server);
    var port = process.env.PORT || 3000;
    server.listen(port);
    console.log('Server running *:'+port);

//routing
app.get('/', function(req, res) {
    res.sendFile(__dirname + '/messages.asp');
});

And if I run server.js in the node terminal I get "Server running:3000" but when I try to load my index page I get the error? I have no idea what Im doing wrong at this stage, I have tested so many things I can't remember what I have tried or not!

So any input really appreciated, thanks!

SOLVED!! So it turned out that you can't have the IP number in the link to the socket.io.js file!!! It has to be the domain. So it has to be <script src="https://somedomain.com:3000/socket.io/socket.io.js">

标签: socket.io
0条回答
登录 后发表回答