Answer the question
In order to leave comments, you need to log in
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>
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');
});
Answer the question
In order to leave comments, you need to log in
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);
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 questionAsk a Question
731 491 924 answers to any question