When i dont get any proper answer so i asked this question that i created a chat app for my site using socket.io and node.js my site is on https server it's not working it's giving me error
GET https://example.com:3000/socket.io/?EIO=3&transport=polling&t=1507034613131-2 net::ERR_INSECURE_RESPONSE
CODE IS HERE
var app = require('express')();
var fs = require('fs');
var path = require('path');
var forceSsl = require('express-force-ssl');
app.use(forceSsl);
var options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
};
var server = require('https').createServer(options, app).listen(3000,function(){
console.log("Https server started on port 3000");
});
var io = require('socket.io').listen(server);
i find above code for https authentication so how can i get these files i have already installed ssl.
i think you can check in your cpanel there is ssl manager which is cpanel service so you can get from there after that you can use in your https nodejs service
Related
I have made a node server with with express and sockets io. It is a simple chat application that would be an addon, to my existing project on my website.
I am having this issue where I am unable to connect to the server from the client to be able to use the chat application.
Error: polling-xhr.js:202 GET https://128.127.106.17:6379/socket.io/?EIO=4&transport=polling&t=Nixndgr net::ERR_CONNECTION_TIMED_OUT
SERVER
const fs = require("fs");
const path = require('path');
const express = require('express');
const app = express();
const http = require('http');
const options = {
key: fs.readFileSync(PATH_TO_KEY),
cert: fs.readFileSync(PATH_TO_CERT),
}
const server = http.createServer(app);
const cookie = require('cookie');
const { Server } = require("socket.io");
const io = new Server(server);
CLIENT
var socket = io(HOSTNAME, {'transports': ['websocket', 'polling']});
With these settings it works perfectly fine on my local environment, but with the actually website I have trouble getting it connected.
The few things I have attempted
I tried adding the ssl options , "const server = http.createServer(options, app);"
I played around with CORS as well and different settings.
Found this https://docs.cpanel.net/knowledge-base/web-services/how-to-install-a-node.js-application/
And I found that the server was running and I would get the hello world but still not able to connect to it and starting to get stumped and running out of resources/possible solutions.
Here is my socket.io server.js file.
var fs = require('fs');
var https = require('https');
var httpsServer = https.createServer({
key:fs.readFileSync("/etc/nginx/ssl/example.com/server.key").toString(),
cert:fs.readFileSync("/etc/nginx/ssl/example.com/server.crt").toString(),
});
httpsServer.listen(9091);
io = require('socket.io').listen(httpsServer);
I run node server.js on server and I tried to go to here:
https://example.com:9091/socket.io/socket.io.js
But it didn't work. (This site can't be reached)
When I did it like as following with http, it worked well.
(http://example.com:9091/socket.io/socket.io.js)
var http = require('http');
var httpServer = http.createServer();
httpServer.listen(9091);
io = require('socket.io').listen(httpServer);
I know we can configure it with nginx reverse proxy too but
what I want to is to setup it without nginx reverse proxy.
Can anyone help me? Thanks in advance.
I'm trying to establish a WebSocket (with node.js/socket.io) on an
HTTPS protocol. But the client still keeps on polling and could not find the server although the server seems fine and listens.
This is what I have done so far. Could you see anything wrong with it?
My assumption is there is something not right with the certificates which I am using. I encrypted the server with plesk "lets encrypt" and took the certificates from this procedure is that right?
---- server side ----
var fs = require('fs');
var express = require('/opt/plesk/node/7/bin/node_modules/express');
var https = require('https');
var app = express();
var server = https.createServer({
key: fs.readFileSync('file.pem'),
cert: fs.readFileSync('file.crt')
},app);
var io = require('/opt/plesk/node/7/bin/node_modules/socket.io').listen(server);
server.listen(8080);
---- client side -----
var socket = io('/', {rejectUnauthorized: false, secure:true});
This is what the client gives me continuously:
https://foo.de:8080/socket.io/?EIO=3&transport=polling&t=MF9zjE6
Since you don't tell what error message you get it's a little hard to identify your problem. Perhaps you should try getting your socket to work and then afterwards try to implement https.
Maybe you should try and simplify it a bit like this.
Server:
var express = require('express')
var app = express()
var server = require('http').Server(app)
var io = require('socket.io')(server)
server.listen(8080);
Client:
var socket = io();
This piece of code should create your socket and your express app. Then you could try and change http to https and it should work aswell.
var app, certificate, credentials, express, fs, http, httpServer, https, httpsServer, privateKey;
fs = require('fs');
http = require('http');
https = require('https');
privateKey = fs.readFileSync('key.pem', 'utf8');
console.log(privateKey);
certificate = fs.readFileSync('cert.pem', 'utf8');
console.log(certificate);
credentials = {
key: privateKey,
cert: certificate
};
express = require('express');
app = express();
httpServer = http.createServer(app);
httpsServer = https.createServer(credentials, app);
httpServer.listen(80);
httpsServer.listen(443);
I am on OS X and I have confirmed nothing else is listening on 80 and 443. I run this as sudo and when I go http://127.0.0.1, it works. However, when I go to https://127.0.0.1, I get not found.
What am I doing incorrect?
To enable your app to listen for both http and https on ports 80 and 443 respectively, do the following
Create an express app:
var express = require('express');
var app = express();
The app returned by express() is a JavaScript function. It can be be passed to Node’s HTTP servers as a callback to handle requests. This makes it easy to provide both HTTP and HTTPS versions of your app using the same code base.
You can do so as follows:
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = express();
var options = {
key: fs.readFileSync('/path/to/key.pem'),
cert: fs.readFileSync('/path/to/cert.pem')
};
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
For complete detail see the doc
add the following line of code:
app.listen(443);
Also, try getting rid of all of the http module, as express handles most of that for you. Look at the beginning Hello World for Express, http://expressjs.com/starter/hello-world.html and look for the part where it handles the port.
I'm pretty much following this tutorial (all other tutorials I found look the same)
http://www.hacksparrow.com/express-js-https.html
My code is as follows:
// dependencies
var express = require('express')
, https = require('https')
, fs = require('fs');
var privateKey = fs.readFileSync('./ssl/rp-key.pem').toString();
var certificate = fs.readFileSync('./ssl/rp-cert.pem').toString();
var app = express.createServer({
key : privateKey
, cert : certificate
});
...
// start server
https.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
The app starts fine after sudo node app
Express server listening on port 443
Now when I curl
curl https://localhost/
I get
curl: (35) Unknown SSL protocol error in connection to localhost:443
Any ideas?
Since Express 3.x, which is now being published via npm, the "app()"-Application Function changed. There is an migration info on https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x . None of the express 2.x SSL tutorials will work anymore. The correct Code for express 3.x is:
// dependencies
var express = require('express')
, https = require('https')
, fs = require('fs');
var privateKey = fs.readFileSync('./ssl/rp-key.pem').toString();
var certificate = fs.readFileSync('./ssl/rp-cert.pem').toString();
var options = {
key : privateKey
, cert : certificate
}
var app = express();
...
// start server
https.createServer(options,app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});