Express ssl address - node.js

I am building an express app that will run on my local network. I just started using a self signed certificut and an https server instead of just an http server. Before I implemented https, I could go to my app with 192.168.1.66 (local ip). But after implementing https, I now have to type https://192.168.1.66:80, otherwise my browser says "connection was reset" or something similar.
Below is my server creation code:
var port = process.env.PORT || 80;//is always 80 since I have not
//set process.end.PORT
var server = https.createServer(sslOptions, app).listen(port, function(){
console.log("listening on port 80");
});
Any ideas why?

It looks like this was a firewall issue on linux. I had turned off ufw, but failed to restart afterwards. After restarting and confirming that the firewall was off, it worked fine :)

Related

Nodejs SSL using CloudFlare not working over https

So the problem I'm having is that the client won't connect with the server.js when the server.js is using https.
if I go to "https://mydomainame.com" I get this error in the console of every other browser than brave browser
index.js:83 GET https://serverip:8081/socket.io/?EIO=3&transport=polling&t=NK0oCD6 net::ERR_CERT_AUTHORITY_INVALID
(The blacked out is the IP address of the server)
the weird thing is that in the brave browser the domain changes to "http://mydomainame.com" and the client then is connected to server.js
I'm using free Cloudflare with Full end to end encryption
server.js code:
var express = require('express'),
https = require('https');
var app = express();
var fs = require('fs');
var httpsOptions = {
key: fs.readFileSync('/var/www/ssl/sitename.com.key'),
cert: fs.readFileSync('/var/www/ssl/sitename.com.pem')};
var server = https.createServer(httpsOptions,app);
var io = require('socket.io').listen(server);
const port = 8081;
server.listen(port);
And client.js connection code:
socket = io.connect('https://serverip:8081', {secure: true});
I am using the same Origin Certificates for the server and for the nodejs code.
The server is using Apache2 with PHPMyAdmin and is configured to make the domain only work using https.
I read somewhere something Cloudflare not being able to use other ports than 443 and some other but I did not really understand it, And I can't get the server.js to work over port 443.
I'm thankful for any information or help I can get! :)
So I figured it out, big thanks to Eric Wong for pointing out the biggest problem that I was trying to connect to the server using its IP there for not going thru Cloudflare.
Then in this article Identifying network ports compatible with Cloudflare's proxy
you can see what ports Cloudflare allows connections on then, I changed my code to used the https port 8443.
socket = io.connect('https://domainname.com:8443',{secure: true});
then the only thing I had to do was to port forward the new port and everything worked fine!

Making my Node app accessible to the Internet from a local machine

OK so I know I can use cloud hosting and I've done so before but I am doing a demo and I want my node app to be on my local machine but accessible from the internet. Here is how I start the server in the server file
const port = 8080;
var server = http.createServer(app).listen(port, () => {
console.log(`Server listening on port ${port}`);
});
Next I set up port forwarding on my Xfinity gateway such that both port 80 and port 8080 point to my desktop. I know I am connecting to the right device because SSH works from outside of my network on port 22. However when I enter [public IP]:8080 I am unable to receive a response. The only time I receive a response is when I enter 10.0.0.58:8080 which refers to my internal network. Why is this???
I personally like ngrok to do the same thing. It's really easy to setup and I found it really stable.
Give it a go https://ngrok.com
After installing you can simply forward ports like
ngrok http 8080

Connecting to NodeJS server from apache served page

So basically I have a webpage runned by apache on port 1900 and I have a NodeJS server running on port 3000.
Server code:
var express = require('express');
var http = require('http');
//make sure you keep this order
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var port = 3000;
server.listen(port, '192.168.0.105', function(){
console.log('Server started: listening on port '+port+'.');
});
On the webpage a have the following code:
var socket = io('192.168.0.105:3000'); which connects to the NodeJS server when loading the page from the computer that runs the server(my laptop) and apache.
The problem appears when I try to access the webpage from another computer(laptop) connected to the same LAN that the laptop running the server is.
When I access 192.168.0.105:1900 from that laptop, I only see the page that is being loaded from apache but doesn't connect to the NodeJS server, it tries to connect to 192.168.0.105:3000 forever but fails after 1 minute.
How do I resolve this problem?
Thank you.
Making my comment into an answer since it solved the problem.
Windows 7 has a built-in personal firewall by default. You may have to enable connections to port 3000 manually. The router is presumably for access from outside the network. You're talking about accessing from your laptop when on the same LAN so that would more likely be the built-in personal firewall.

AZURE + SOCKET.IO: How do I know which port my Web App is using?

I went to a Hackathon last weekend and a Microsoft recruiter set me up with Azure for my Node.js project.
We used Socket.io with my project and had a hard time connecting the Client to the Server because we didn't know which port to connect to...
On our WebApp (not Azure VM), we had the following code:
var port = process.env.port || 3000;
On the client side of Socket.io, I had to specify an ip address to use along with it's port. I tried:
var socket = io('http://IP.AD.DRE.SSS:3000'); //And
var socket = io('http://IP.AD.DRE.SSS'); //And even a different Port
var socket = io('http://IP.AD.DRE.SSS:9999'); //And 443 and 80
And every iteration... I had to be doing something wrong. We ended up switching over to Digital Ocean because I knew how to use it but I really wanted to get this working.
Any Ideas?
UPDATE:
I changed it to 80 and my current error is: "Access Control Allow Origin." Note: My client is running on a server.
UPDATE 2: Return of the OP
Unfortunately, the CORS package for Node did not do the trick...
Some more info:
I'm not using Express or Connect. My server is on Azure (as an Azure Web App). My Client was on Localhost (Thanks to WebStorm).
Azure Web Apps only listens on ports 80 & 443. Change the port to either of them and your app will work fine.
Just leave the port off in the client string. You can connect using the URL alone. Look at my code I codefoster.com/commandmonkey as an example.

node.js hosting with SSL?

So say I have a node.js application that hosts both a HTTP and HTTPS server as described in the question: How to force SSL / https in Express.js
In my code I have the following:
// General configuration settings for production usage
app.configure(function () {
app.set('port', process.env.PORT || 3000);
app.set('sslport', process.env.SSLPORT || 4000);
...
}
http.createServer(app).listen(app.get('port'), function () {
winston.info('Express server listening on port ' + app.get('port'));
});
var options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
https.createServer(options, app).listen(app.get('sslport'), function () {
winston.info('Express server listening on port ' + app.get('sslport'));
});
Which works perfectly fine for a local running node server.
However, I want to publish my site to a cloud hosted provider like Azure Web Sites, Heroku, Nodejitsu, etc.
All of the cloud hosts seem to set a process.env.PORT value, but only the one. When my HTTPS server is created this usually results in the app crashing, as the PORT is already in use / access denied / etc.
So how do I create / host a site with a secure login page with only one port to work with!?
If you use Heroku you get SSL without needing to specify a port in nodejs. All you need to do is listen on the heroku PORT environment variable for http requests. Once uploaded to heroku you can address your heroku app using either https (on 443) or http (on port 80). Heroku routes either to your server.
Similarly if using elastic load balancing with EC2 you can make use of SSL termination at the load balancer, and again route to your node server listening on port 80 using using http. http://aws.amazon.com/elasticloadbalancing
In both cases you can use either self-signed or proper SSL certificates depending upon your need.

Resources