Socket.IO with Express will not connect using https (Apache2 Ubuntu16.04) - node.js

I am not very experienced with SSL certs and Im hopping someone can help me find out what I am doing wrong.
I am trying to host a NodeJS application with it's own port (*:1729) with Apache2 which has SSL enabled on port 443 (from which it servers a client application and not the NodeJS/Express/Socket.io application in question). When I set up Express with http it works fine, my client application communicates without error to the NodeJS application, however when I use https with express such as this:
this.express = require('express');
this.app = this.express();
var https = require('https');
var fs = require('fs');
var sslPath = '/etc/letsencrypt/live/yourdomain.example.com/';
var options = {
key: fs.readFileSync(sslPath + 'privkey.pem'),
cert: fs.readFileSync(sslPath + 'fullchain.pem')
};
this.server = https.createServer(options, this.app);
this.io = require('socket.io').listen(this.server);
this.server.listen(1729);
When ever my client app tries to connect I get this error:
node: ../src/util-inl.h:196: TypeName* node::Unwrap(v8::Local<v8::Object>) [with TypeName = node::TLSWrap]: Assertion ``(object->InternalFieldCount()) > (0)' failed.
Aborted (core dumped)
Any advice or corrections that could point me in the right direction would be much appreciated, thanks!

Thank you of the help! :) Updating my NodeJS to the latest version made the error disappear and the application is communicating with the client with no error's as well.
node: Github issue # 3682
I just followed these steps.
AskUbuntu: How can I update my nodeJS to the latest version?

Related

Unable to connect nodejs api on localhost:5000 on server

I have successfully deployed React app on digitalocean droplet with mongodb.
My server configured with nodejs nginx pm2 and everything is working fine
here is my site
www.xdomain.ml
But I build my react app to connect with the backend nodejs api.
I am using socket.io and axios to connect.
everything is working fine on my local computer but it could not connect on the server.
my socket configuration is like
const socket = io("localhost:5000")
and axios is like
axios.post("http://localhost:5000/delete-domains")
on browser console, showing
polling-xhr.js:198 GET https://localhost:5000/socket.io/?EIO=4&transport=polling&t=NjNh9TP net::ERR_SSL_PROTOCOL_ERROR
what I am missing?
do I need to replace the localhost:5000 to 127.0.0.1:5000 on code and build again?
But the mongodb connection successfully establish by
"mongodb://localhost:27017/api";
please help
Thanks
Try to change
const socket = io("localhost:5000")
to
const socket = io("http://localhost:5000")
Or other advance setting:
const socket = io.connect('http://localhost', {
port: 5000,
reconnect: true
});
const socket = io("localhost:5000")
on this line you are missing "http://" or if you have ssl "https://"
this line should be
const socket = io("http://localhost:5000");

Issue from HTTPS installation in node js

I am using https package for running the program in https. I have proper ssl certificate and private key and bundle file. all these files included in my code. This https program also running without error. But when I connect front end to node js it doesn't connected. The error is "504 gateway Time out". Where is I make error? I host my code in azure.
const https = require('https');
//path for private key and certificate
let privateKey = fs.readFileSync('sslcert/server.key','utf8');
let certificate = fs.readFileSync('sslcert/server.crt','utf8');
var credential = {
key: privateKey,
cert: certificate,
};
var server = https.createServer(credential, app)
server.listen('8443',function() {
console.log('Listening on https://localhost:' + 8443);
});
Refer to this https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback
So if you do not declare host for your server.listen() the default host should be 0.0.0.0 instead.
If you need to listen of specific address, you must define it after the port.
http.createServer(function (req, res) {
}).listen(8443, "0.0.0.0");
Your front server need to be on the same network subnet as your API.

Solving a Proxy Error (Node.js server)

Basically I get a 502 Proxy Error when running my node.js app.
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /play.
Reason: Error reading from remote server
My server looks like this.
var express = require('express');
var https = require('https');
var http = require('http');
var path = require('path');
var fs = require('fs');
var mysql = require('mysql');
var app = express();
var options = {
key: fs.readFileSync('sslcert/keyfile.key', 'utf8'),
cert: fs.readFileSync('sslcert/crtfile.crt', 'utf8')
};
var httpsServer = https.createServer(options, app);
// stuff
httpsServer.listen(process.env.PORT);
I am really sorry if this is a noob question, actually I am still a beginner in things related to node.js. Thank you for your help!
Noël.
I just ran into the same problem. It's possible your problem was different, but in my case, the 502 error was coming from Apache. My httpd.conf file was referencing the same 2 certificate files that my node app was referencing.
Instead of using
var httpsServer = https.createServer(options, app);
try just
app.listen(3000);
I'm not sure exactly why it wasn't working. My theory is the node app was using these cert files, and apache was unable to access them, and thus creating this situation. However, just using a normal express app fixed the problem for me.
I hope this helps somebody!

Problems installing SSL Certificates on Node.JS server

I bought SSL Certificates from Godaddy for our website, with two web servers on the same AWS EC2 instance - Apache Tomcat and Node/Express.
After installing the SSL Certificates on both the web servers, https://example.com is opening (Apache), but the GET request from Apache to Node is failing.
The GET request to Node is working fine with the self-generated certificate (though it shows a crossed out https). When we replace that with CA certificates, it is not working. Please see the relevant code below.
var https = require("https"); // https server core module
var fs = require("fs"); // file system core module
var express = require("express"); // web framework external module
// Setup and configure Express http server. Expect a subfolder called
“static” to be the web root.
var httpApp = express();
httpApp.configure(function() {
httpApp.use(express.static(__dirname + "/static/"));
});
// Start Express https server on port 443
var webServer = https.createServer(
{
key: fs.readFileSync("/pathtokeys/ssl.private.key"),
cert: fs.readFileSync("/pathtokeys/1.crt"),
cert: fs.readFileSync("/pathtokeys/2.crt"),
cert: fs.readFileSync("/pathtokeys/3.crt"),
passprase:"miljul123$$$"
},
httpApp).listen(4431);
// Start Socket.io so it attaches itself to Express server
var socketServer = io.listen(webServer, {"log level":1});
var rtc = apsrtc.listen(httpApp, socketServer);
Any thoughts or suggestions? Thanks.

Node.js socket.io with ssl connection

I am trying to connect node.js server with socket.io using ssl. My server has windows 2008 server and iis installed. I intalled ssl my domain name on IIS and I can connect with my domain via http s://mydomain.com. But I cant connect my node.js server with ssl. I am using following code on my server,Have you got any ideas what the problem might be?
var fs = require('fs');
var options = {
pfx: fs.readFileSync('sslkey.pfx'),
passphrase:'password'
};
var express = require('express'),
app = express(),
server = require('https').createServer(options,app),
server2 = require('http').createServer(app),
io = require('socket.io').listen(server, {log: true});
function sendCrossDomain(req, res){
//return;
res.set('Content-Type', 'text/xml; charset=utf-8');
res.sendfile(__dirname + '/crossdomain-test.xml');
};
app.get('/crossdomain.xml', sendCrossDomain);
app.get('/', sendCrossDomain);
server.listen(9595);
Assuming there are no errors about your certificate when you run the node program,
I would check to make sure the firewall port is opened on TCP 9595.
You will also have to request the page with https://yourdomain.com:9595 in order to access the running program since it's SSL and on a different port than 443.

Resources