HTTP and HTTPS same time (Express.Io) - node.js

I'm successfully listenin port 443 and can access server over https, but I can't access it with http.
var fs = require('fs')
options = {
ca : fs.readFileSync('./ssl/site.com.pem'),
key: fs.readFileSync('./ssl/site.com.key'),
cert: fs.readFileSync('./ssl/site_com.crt')
}
var app = require('express.io')
app.https(options).io()
....
app.listen(443);
I've tried using http and https modules:
app.http().io();
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
But this time socket.io is giving 404 in browser. How can I solve this? I need to use Express.Io's socket connection because application is based on it.

You should redirect http to https
var express = require('express'),
app = express(),
httpapp = express();
//........................
var credentials = {key: privateKey, cert: certificate, ca: ca};
var httpsServer = https.createServer(credentials, app);
var httpServer = http.createServer(httpapp);
httpsServer.listen(443);
httpServer.listen(80);
httpapp.route('*').get(function(req,res){
res.redirect('https://yourdomain.com'+req.url)
});

Had the same problem a few days ago, and this GitHub issue helped:
https://github.com/techpines/express.io/issues/17#issuecomment-26191447
Your code is on the right way, it just need some changes. The code below is a slightly modified version of the snippet you provided.
var fs = require('fs'),
express = require('express.io');
options = {
ca : fs.readFileSync('./ssl/site.com.pem'),
key: fs.readFileSync('./ssl/site.com.key'),
cert: fs.readFileSync('./ssl/site_com.crt')
};
var app = express();
app.https(options).io();
var httpServer = require('http').createServer(app);
// ...
app.listen(443);
express.io.listen(httpServer);
httpServer.listen(80, function() { }, function() { });

Related

Node.js, socket.io https connection

Server side code:
var io = require('socket.io').listen(8150);
io.sockets.on('connection', function (socket){
});
Client side code:
var socketIO = io('*.*.*.*:8150');
socketIO.once('connect', function(){
});
On http it's worked on https in same page it not connected.
Searched many examples, but all example for express. I dont create any http server in node.js need only to socket.io work.
When running the client over HTTPS, socket.io is attempting to connect to your server over HTTPS as well. Currently your server is only accepting HTTP connections, the listen(port) function does not support HTTPS.
You'll need to create an HTTPS server and then attach socket.io to it, something like this.
var fs = require('fs');
var options = {
key: fs.readFileSync('certs/privkey.pem'),
cert: fs.readFileSync('certs/fullchain.pem')
};
var app = require('https').createServer(options);
var io = require('socket.io').listen(app);
app.listen(8150);
io.sockets.on('connection', function (socket) {
});
And if you need both HTTP and HTTPS, you can start two servers and attach socket.io to both.
var fs = require('fs');
var options = {
key: fs.readFileSync('certs/privkey.pem'),
cert: fs.readFileSync('certs/fullchain.pem')
};
var httpServer = require('http').createServer();
var httpsServer = require('https').createServer(options);
var ioServer = require('socket.io');
var io = new ioServer();
io.attach(httpServer);
io.attach(httpsServer);
httpServer.listen(8150);
httpsServer.listen(8151);
io.sockets.on('connection', function (socket) {
});
Then on the client side you can determine which port to connect to based on whether the page was accessed over HTTP or HTTPS.
var port = location.protocol === 'https:' ? 8151 : 8150;
var socketIO = io('*.*.*.*:' + port);
socketIO.once('connect', function() {
});
Use letsencrypt with Plesk for a valid SSL certificat.
options = {
key: fs.readFileSync('/usr/local/psa/var/modules/letsencrypt/etc/live/mydomain.com/privkey.pem'),
cert: fs.readFileSync('/usr/local/psa/var/modules/letsencrypt/etc/live/mydomain.com/cert.pem'),
ca: fs.readFileSync('/usr/local/psa/var/modules/letsencrypt/etc/live/mydomain.com/chain.pem'),
rejectUnauthorized: false,
requestCert: true,
agent: false
}

How do I listen on port 443 using Express?

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.

expressjs 4.x and https, generate key and certificate

I want to create an https server with express 4.x. Even if a lot of code found on google is based on express 3.x I think I made the port correctly.
Even if I tried to goole it is not very clear to me how to generate the keys. Without the key I'm expecting 401.
I tried with the script found in this gist. But I'm keeping on receiving the error Error: DEPTH_ZERO_SELF_SIGNED_CERT.
I'd like to test it both with curl, request, and super test.
This is what actually I have:
server.js
var express = require('express')
, https = require('https')
, fs = require('fs');
var privateKey = fs.readFileSync('./server/server-private-key.pem').toString();
var certificate = fs.readFileSync('./server/server-certificate.pem').toString();
var options = {
key : privateKey
, cert : certificate
}
var app = express();
app.get('/', function(req, res) {
req.client.authorized ?
res.json({"status":"approved"}) :
res.json({"status":"denied"}, 401);
});
server = https.createServer(options,app);
var port = 12345;
server.listen(port, function(){
console.log("Express server listening on port " + port);
});
client.js
var https = require('https');
var fs = require('fs');
var options = {
host: 'localhost',
port: 12345,
method: 'GET',
path: '/',
key: fs.readFileSync('./client/client-private-key.pem'),
cert: fs.readFileSync('./client/client-certificate.pem'),
headers: {}
};
var req = https.request(options, function(res) {
console.log('dudee');
console.log(res);
});
req.end();
With cURL you can use the -k flag to bypass the self-signed cert problem.
With request you can just set rejectUnauthorized: false in the request options.

Can't connect to Socket IO with SSL

Server:
// Load libraries
var https = require('https');
var fs = require('fs');
var socketio = require('socket.io');
// The server options
var srvAddress = '123.123.123.123';
var srvPort = 8888;
var srvOptions = {
key: fs.readFileSync('ssl/cert.key'),
cert: fs.readFileSync('ssl/cert.crt'),
ca: fs.readFileSync('ssl/cert-ca.crt')
};
// Create a Basic server and response
var app = https.createServer(srvOptions, function(req, res) {
res.writeHead(200);
res.end('Online...');
});
// Create the Socket.io Server over the HTTPS Server
var io = socketio.listen(app, srvAddress);
// Now listen in the specified Port
app.listen(srvPort, srvAddress);
Client:
var socket = require("socket.io-client").connect('https://123.123.123.123:8888', {secure: true});
But client don't connect to server. I tried http instead of https, but the same result.
Server works fine. Without ssl certificate connection works. When try to open via web, then all works too.

How do I setup a SSL certificate for an express.js server?

Before, in an older version of express, I could do this:
express.createServer({key:'keyFile', cert:'certFile'});
However, in newer versions of express this no longer works:
var app = express();
Should I call app.use() to set the certs? If so how?
See the Express docs as well as the Node docs for https.createServer (which is what express recommends to use):
var privateKey = fs.readFileSync( 'privatekey.pem' );
var certificate = fs.readFileSync( 'certificate.pem' );
https.createServer({
key: privateKey,
cert: certificate
}, app).listen(port);
Other options for createServer are at: http://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
I was able to get SSL working with the following boilerplate code:
var fs = require('fs'),
http = require('http'),
https = require('https'),
express = require('express');
var port = 8000;
var options = {
key: fs.readFileSync('./ssl/privatekey.pem'),
cert: fs.readFileSync('./ssl/certificate.pem'),
};
var app = express();
var server = https.createServer(options, app).listen(port, function(){
console.log("Express server listening on port " + port);
});
app.get('/', function (req, res) {
res.writeHead(200);
res.end("hello world\n");
});
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.
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);

Resources