My code is hosted on the GitHub Pages. I'm trying to connect client to my computer's Node.JS server by IP, but connection says an error ERR_SSL_VERSION_OR_CIPHER_MISMATCH.
That's my server code:
var app = require('express')();
var server = require('https').createServer(app);
var io = require('socket.io')(server);
app.set('port', process.env.PORT || 8080);
app.set('host', process.env.HOST || '0.0.0.0');
server.listen(app.get('port'), app.get('host'), () => {
console.log("Express server listening IP: " + app.get('host'));
});
io.on('connection', socket => {
socket.on('sayHi', data => {
console.log(data.message);
});
});
And this is client side:
const socket = io.connect('https://myPublicIP:8080');
socket.emit('sayHi', { message: 'Hello world' });
That error happen because you using HTTPS for running SocketIO Server.
So we have two way for resolve this issue:
Change require('https') to require('http')
Import your Certificate SSL files when createServer following this example source code
const privateKey = fs.readFileSync(process.env.PRIVATE_KEY, 'utf8')
const certificate = fs.readFileSync(process.env.CERTIFICATE, 'utf8')
const credentials = {
key: privateKey,
cert: certificate,
passphrase: process.env.PASSPHRASE
}
var server = require('https').createServer(credentials, app);
Hope this can help you!
Related
i am trying to send an https request from my frontend (reactjs) to backend (nodejs/express).
These two both run in localhost.
Back end server code:
const app = require('./app')
const https = require('https');
const fs = require('fs');
const credentials = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
//connect to the database
require('./db')
const port = 8765;
app.get('/', (req, res) => {
res.send('Now using https..');
});
var server = https.createServer(credentials, app);
//var server = https.createServer(app);
// listen for requests
server.listen(port, () => {
console.log("server starting on port : " + port)
});
front end request:
const {data: Sessions}= await axios.get("https://localhost:8765/...");
i am trying to send an https request from my frontend (reactjs) to backend (nodejs/express).
These two both run in localhost.
Back end server code:
const app = require('./app')
const https = require('https');
const fs = require('fs');
const credentials = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
//connect to the database
require('./db')
const port = 8765;
app.get('/', (req, res) => {
res.send('Now using https..');
});
var server = https.createServer(credentials, app);
//var server = https.createServer(app);
// listen for requests
server.listen(port, () => {
console.log("server starting on port : " + port)
});
front end request:
const {data: Sessions}= await axios.get("https://localhost:8765/...");
doing this request from postman with the exact same parameters produces the desired result.However when i try to do this from frontend i get: GET https://localhost:8765/... net::ERR_CERT_AUTHORITY_INVALID in react chrome extention.I believe this is because i am using a self signed certificate and chrome browser can't verify it's validity.
Is there a way to temporarily disable this verification step from chrome?
If not how else can i solve this?
Not : Doing this with HTTP works fine but i need it to be HTTPS.
If your just going to run it on local host one your machine you can disable the setting at chrome://flags/#allow-insecure-localhost in browser.
This will not fix anything in production tho, only for personal use.
I am running my server on ionos hosting and executing nodejs on the default port of 80.
I don't know how to enable the HTTPS for it.
Following is my sample node js server creation code:
const Https = require('https');
const fs = require('fs');
const httpsServer = Https.createServer({
key: fs.readFileSync("private.key"),
cert: fs.readFileSync("Dev-2020-09-12-013930.cer")
}, app);
var io = require('socket.io').listen(Https);
global.SOCKET = io;
const ip = require('ip');
console.log('websocket server start.' + ' ipaddress = ' + ip.address() );
// const socket = io('http://localhost:5000');
httpsServer.listen(80, function () {
console.log('Server port: ' + port);
});
I have generated certificates and added them. On running the server it gives message of server started but does not load on browser.
Try adding these lines of code and see if you get "Hello" text in your browser.
https.get("/", (req, res) => {
res.send("Hello");
});
if that didn't work try doing it this way
httpsServer.get("/", (req, res) => {
res.send("Hello");
});
EDIT
Check out the official documentation https://nodejs.org/api/https.html
I have 2 node.js servers running;
1 on port 8000
another on port 8080
The one on port 8080 will be an API, so need to send POST requests to it.(Endpoint: websocket/test).
When I try to do this, a 404 gets returned.
It is sat in a subdirectory(ROOT/webhook), so not sure if that is the reason, or if its the fact that it is on port 8080?
Socket.io is working fine and connects with no issues, I just cant send a POST request to this server.
Here is the server.js file:
//SOCKET.IO Server
const express = require('express');
const app = express();
const port = 8080;
const fs = require('fs');
const http = require('http');
const https = require('https');
const sslKey = 'HIDDEN';
const sslCert = 'HIDDEN';
const options = {
key: fs.readFileSync(sslKey),
cert: fs.readFileSync(sslCert)
};
const httpServer = http.createServer();
const httpsServer = https.createServer(options);
const io = require('socket.io')(httpsServer);
// FOR HTTP
// httpServer.listen(port, () => {
// console.log("Socket.io http server is listening on port: " + port)
// console.log(__dirname + '/key.pem');
// })
// FOR HTTPS
httpsServer.listen(port, () => {
console.log("Socket.io https server is listening on port: " + port);
})
io.on('connection', function(socket){
console.log('made socket connection', socket.id);
socket.emit('welcome', 'Hiya! Welcome');
app.post('/websocket/test', function() {
console.log('webhook received');
io.emit('webhook', 'You have received a webhook!');
});
});
You almost got everything right. Except you aren't telling your express application to start listening for requests. By just changing this code:
const httpsServer = https.createServer(options);
to this:
const httpsServer = https.createServer(options, app);
Your server should work.
I'm comfronted with a big problem with socket.io ans NodeJS.
I try to communicate with the NodeJS server through Socket.IO under HTTPS, but when it's time for the server to send a response to the client, it doesn't do anything.
I've checked, but it's not a certificate error ! Key and certificate are valid.
However, without HTTPS, it works perfectly.
Here is my code:
var express = require('express'),
fs = require('fs');
var config = JSON.parse(fs.readFileSync(__dirname + '/config.json', 'utf8')),
options = config.options || {};
var PORT = options.port,
SSL_PORT = options.ssl_port;
var app = express(),
server = require('http').createServer(app);
io = require('socket.io').listen(server);
server.listen(PORT, function()
{
startServer(io);
});
if (options.ssl_key.length && options.ssl_cert.length)
{
var sslOpt = {
key: fs.readFileSync(options.ssl_key, 'utf8'),
cert: fs.readFileSync(options.ssl_cert, 'utf8')
};
var sslServ = require('https').createServer(sslOpt, app),
ioSsl = require('socket.io').listen(sslServ);
sslServ.listen(SSL_PORT, function()
{
startServer(ioSsl);
});
}
function startServer(io)
{
io.on('connection', function(socket)
{
socket.on('msg', function(data)
{
io.emit('clientMsg', 'New message !');
});
}.bind(this));
}
And the client part:
var PORT = 2999,
secured = false,
serverName = 'my-website-url',
completeUrl = 'https://my-website-url.com';
if (completeUrl.substr(0, 5) == 'https')
{
PORT = 3000;
secured = true;
}
var socket = io(serverName + ':' + PORT, {
transports: ['websocket'],
secure: secured
});
socket.emit('msg', 'I send a msg');
I use port 2999 for HTTP and 3000 for HTTPS, when I send a message through the HTTPS server (port 3000), I don't receive the server response, but I receive it on HTTP server (port 2999).
So, under SSL, the server doesn't send a response to clients who are using my HTTPS website.
is there any example about secure inter-server communication in node.js?
Is socket.io necessary to realize this or I may use express too?
I would like to realize mutual authentication between two servers.
Thanks to all!!!
This is an example of what i've done; unfortunately it doesn't work...
Server 1
...
var ioc = require('socket.io-client');
var socketc = ioc.connect("https://127.0.0.1:5000", {'force new connection':true, 'secure':true});
...
Server 2
var fs = require('fs');
var https = require('https');
var io = require('socket.io');
var crypto = require('crypto');
// path to private key and certificate
var pathSSH = 'C:\\cert\\';
//array containing private key and certificate
var options = {
key: fs.readFileSync(pathSSH + 'keySatellite.pem'),
cert: fs.readFileSync(pathSSH + 'keySatellite-cert.pem')
};
var port = 5000;
var server = https.createServer(options, function(req, res) {
console.log('Connection with satellite node');
});
var sio = io.listen(server);
// ...listening at port XXXXX
server.listen(port, function(){
console.log("HTTPS server listening on port " + port);
});
sio.sockets.on('connection', function(socket) {
console.log('connected');
socket.disconnect();
});
UPDATE 1
I've noticed that server to server communication works fine if I set a http connection instead of https.