Problems installing SSL Certificates on Node.JS server - node.js

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.

Related

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.

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

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?

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!

How to enable SSL connection a NodeJS (Express) based Server

Following is the script I found on NodeJS Official Website:
// curl -k https://localhost:8000/
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
I'm completely new to SSL thing. I read some tutorials on how to enable SSL on Nodejs but still quite not confident with the process.
I have a domain name (Godaddy), SSL (Namecheap) and Cloud Server (Digital Ocean with an application deployed on HTTP prefix).
Whenever I open my Login page of my website, Google Chrome mark it as "Not secure" so I wanted to add SSL to the website.
What things I need to do on the NodeJS server (Express) and what things I need to do on Namecheap? What will be the sequence of doing that? What are cmd lines to generate .pem .csr files?
I'm didn't found and comprehensive guide laying down all the steps in a concise manner so just laid down the steps concisely (if possible) with the links to the resources for further digging.
And also, how can I use express framework to create https server in liue of above script?
That script is correct for setting the certs for your https. If your site is public, as it seems, then you'll want to buy certs from your ssl service, Namecheap in your example. Then you would move them to your host and reference them in the options object in your example. However, you can generate your own ssl certs and that will work as well. Though, any users will be warned that they're not trusted since you self signed/created them. I suggest going with the Namecheap option.
Note: You only have an https server in your example and anyone attempting to access your site via http will receive a blank page. You'll need to also create an http server, via the following:
var http = require('http');
http.createServer(...);
I would suggest having the http server simply redirect to the https url.
My code and error is here:
no such directory found error:
key: fs.readFileSync('../private.key'),
cert: fs.readFileSync('../public.cert')
error, no such directory found
key: fs.readFileSync('./private.key'),
cert: fs.readFileSync('./public.cert')
Working code should be
key: fs.readFileSync(__dirname + '/private.key', 'utf8'),
cert: fs.readFileSync(__dirname + '/public.cert', 'utf8')
Complete https code is:
const https = require('https');
const fs = require('fs');
// readFileSync function must use __dirname get current directory
// require use ./ refer to current directory.
const options = {
key: fs.readFileSync(__dirname + '/private.key', 'utf8'),
cert: fs.readFileSync(__dirname + '/public.cert', 'utf8')
};
// Create HTTPs server.
var server = https.createServer(options, app);
This is my working code for express 4.0.
express 4.0 is very different from 3.0 and others.
4.0 you have /bin/www file, which you are going to add https here.
"npm start" is standard way you start express 4.0 server.
readFileSync() function should use __dirname get current directory
while require() use ./ refer to current directory.
First you put private.key and public.cert file under /bin folder,
It is same folder as WWW file.

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