N
N
nastyskafomka2019-04-09 16:16:31
Node.js
nastyskafomka, 2019-04-09 16:16:31

How to connect to node js ssl from cloudflare?

Hello, I am making a server for real-time notifications on socket.io.

This server is located in the domain.ru domain. I

connect to this server on the tech.domain.ru subdomain (for testing)

Client html code

<!doctype html>
<html>
<head>
    <title>Socket.IO chat</title>
    <script src="//domain.ru/socket.io/socket.io.js"></script>
    <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
        $(function () {
            var socket = io.connect('https://VPSip:2096', {rejectUnauthorized: false, secure: true});
            $('form').submit(function (e) {
                e.preventDefault(); // prevents page reloading
                socket.emit('chat message', $('#m').val());
                $('#m').val('');
                return false;
            });
            socket.on('chat message', function (msg) {
                $('#messages').append($('<li>').text(msg));
            });
            socket.on('connect', () => {
                console.log(socket.id);
            });
        });
    </script>
</head>
<body>
<ul id="messages"></ul>
<form action="">
    <input id="m" autocomplete="off"/>
    <button>Send</button>
</form>
</body>
</html>


Server part:
var fs = require('fs');
var app = require('express')();

var http = require('https').createServer(app);

var io = require('socket.io').listen(http)

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
    console.log('a user connected');
    console.log('User id: '+socket.id);
    socket.on('disconnect', function () {
        console.log('user disconnected');
    });
    socket.on('chat message', function(msg){
        console.log("message: "+msg);
        io.emit('chat message', msg);
    });
});
app.post('/send',function (req, res) {
    console.log(res.data);
});
io.emit('some event', { for: 'everyone' });

http.listen(2096, function () {
    console.log('listening on *:2096');
});


When opening the developer console, I see:
GET https://VPSip:2096/socket.io/?EIO=3&transport=poll... net::ERR_SSL_VERSION_OR_CIPHER_MISMATCH

Where VPSip is the ip address of my vps server.

ssl from cloudflare

What to do, please help. I've been searching for two days and can't find a solution.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nastyskafomka, 2019-04-10
@nastyskafomka

Problem solved.
As always, inattention is to blame, and the desire "all at once."
In order to connect a certificate from Cloudflare to a node, you need to do the following:
1) Generate a TLS certificate from Cloudflare. To do this, go to the domain control panel, then Crypto and there we find the Origin Certificates
item 2) Create a TLS certificate by selecting the following options:
(You can choose a different term)
3) Now create a folder on the server where our certificates will be located and create files in it:
your-domain.ltd.pem and your-domain.ltd.key
Where your-domain.ltd is your domain.
4) Insert the first key into your-domain.ltd.pem
5) Insert the second key into your-domain.ltd.key
6) Next, on the server, connect them:

var app = require('express')();

var options = {
    key: fs.readFileSync('ssl/your-domain.ltd.key'), // PRIVATE KEY
    cert: fs.readFileSync('ssl/your-domain.ltd.pem') // CERTIFICATE
};

var http = require('https').createServer(options,app);

Next, you need the server to work on the SSL port that Cloudflare uses ( link )
Может это не обязательно, но мы сделали именно так
На http не обращайте внимания, просто так получилось.
That's all, now you can start the node.js server and navigate to your-domain.ltd:port
And we will successfully get on the server.
Hope this helps someone.

B
Boris Korobkov, 2019-04-09
@BorisKorobkov

var fs = require('fs');

var options = {
  key: fs.readFileSync('ssl/privkey.pem'),
  cert: fs.readFileSync('ssl/fullchain.pem')
};

var httpServer = require('http').createServer();
var httpsServer = require('https').createServer(options);
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question