I try to use express in node.js with https.
Here the relevant code for this parts:
var express = require("express");
var app = express();
var https = require('https');
var privateKey = fs.readFileSync('./sslcert/mykey.key', 'utf8');
var certificate = fs.readFileSync('./sslcert/mssl.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
https.createServer(credentials, app).listen(5008, function(res){
console.log("Listen to: " + port)
});
In the console, it prints Listen to: 5008, but I don't see that my server get any request, even though I sent some.
When I use express without the ssl extension, everything works fine.
EDIT:
Here the code which works OK with port 5008, but without the ssl part:
var express = require("express");
var app = express();
app.listen(5008, function () {
console.log("Listening on " + port);
});
What I do wrong?
You have to specify https://, otherwise the browser/client will not know to use SSL when connecting, no matter what port is used (the default of 443 or otherwise).
Related
I am using Node and express.js to build my server and I setup my https server (essentially) like this:
const privateKey = fs.readFileSync('./credentials/rippal.key', 'utf8');
const certificate = fs.readFileSync('./credentials/rippal.crt', 'utf8');
const credentials = {key: privateKey, cert: certificate};
const app = express();
setup(app, config);
let httpsServer = https.createServer(credentials, app)
httpsServer.listen(3333, function () {
console.log("Server listening on port 3333...\n");
});
with self-signed certificates, it still works to http://localhost:3333 but when I tried to access https://localhost:3333, Chrome told me
This site can’t provide a secure connection
localhost sent an invalid response.
ERR_SSL_PROTOCOL_ERROR
how should I access https://localhost:3333?
Thanks!
I'm having trouble with being able to connect to my node.js server from an external domain. It works fine when running it locally using the http web server through node however when connecting externally, it loads the socket.io.js file just fine but when trying to use the socket it removes the port from the URL and cannot connect.
Instead of doing this in the network requests:
http://external-domain.com:3000/socket.io/?EIO=3&transport=polling&t=M06GOUU
it does this:
http://external-domain.com/socket.io/?EIO=3&transport=polling&t=M06GOUU
I'm not sure how to make it not remove the port from the connection. How do I go about fixing this?
SERVER
const path = require('path');
const http = require('http');
const express = require('express');
const socketIO = require('socket.io');
const publicPath = path.join(__dirname, '../public');
var app = express();
var server = http.createServer(app);
var io = socketIO(server);
app.use(express.static(publicPath));
server.listen(3000, () => {
console.log(`Server is up on port 3000`);
});
CLIENT SCRIPT TAG
<script src="http://external-domain.com:3000/socket.io/socket.io.js"></script>
CLIENT JS ON A DIFFERENT DOMAIN
var socket = io();
socket.connect('http://external-domain.com:3000');
socket.on('connect', function () {
console.log('Connected to server.');
});
Change from this:
var socket = io();
socket.connect('http://external-domain.com:3000');
to just this:
var socket = io("http://external-domain.com:3000");
And, you don't use the socket.connect() as you will already have requested the connection with the io("http://external-domain.com:3000"); call.
Explanation
The code:
var socket = io();
uses the page URL to connect to a socket.io server at that origin. That is not what you want (apparently).
If you wanted to use the .connect() method, it would be like this:
var socket = io.connect("http://external-domain.com:3000");
Note: var socket = io(url) is simply a shortcut for var socket = io.connect(url).
socket.connect() does not accept a URL as a parameter so you simply weren't using that correctly. It's just a synonym for socket.open().
Use io.connect("url")
var socket = io.connect("http://external-domain.com:3000", { rejectUnauthorized: false });
// { rejectUnauthorized: false } is an optional parameter.
Hope this works for you.
Is it somehow possible to create a Node.js https server in cloud9 IDE?
Below is my example of simple https server setup in Node.js.
var https = require('https');
var fs = require('fs');
var app = require('./app');
// SSL Configuration
var ca_names = ['CERT-NAME_1', 'CERT-NAME_2', 'CERT-NAME_3'];
var options = {
key: fs.readFileSync('./folder/server.key'),
cert: fs.readFileSync('./folder/server.crt'),
ca: ca_names.map(function(n) {
return fs.readFileSync('./eid/ca/' + n + '.crt');
}),
//crl: ca_names.map(function(n) { return fs.readFileSync('/eid/ca/' + n + '.crl'); }),
requestCert: false,
rejectUnauthorized: false
};
var server = https.createServer(options, app);
server.listen(process.env.PORT || 8081, process.env.IP || "0.0.0.0");
console.log('server listening on port: ' + process.env.PORT);
when I try to connect to the server then I am getting following error:
"ECONNRESET: Request could not be proxied!"
I think the problem is you are trying to listen to both HTTP and HTTPS.
c9 works as a proxy so you only need to listen on HTTP even though you are trying to use HTTPS. Try not listening to HTTPS and it should work. (more info on this)
But, if you really need HTTPS, in that case you can use a proxy like Nginx to internally proxy requests over HTTPS.(more info on this)enter link description here
I have created following certificate and key(self-signed). When I run the app, the browser loads with Not secure and https is striked off. It should run on https right when the key and cert is available?
Code:
var express = require('express');
var app = express();
var fs = require('fs');
var http = require('http');
var https = require('https');
var request = require('request');
var rp = require('request-promise');
var port = process.env.PORT || 5026;
app.set('port', (port));
app.use('/', express.static(__dirname + '/public_html'));
var options = {
key: fs.readFileSync('./server.key', 'utf8'),
cert: fs.readFileSync('./server.crt', 'utf8')
};
http.createServer(app).listen(8080);
https.createServer(options, app).listen(8443);
There's no error in console though. I have never worked with ssl before. What am I missing here?
Update: Added CA Certificate and private key
But now I get this error:
Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
at Error (native)
at Object.createSecureContext (_tls_common.js:65:17)
at Server (_tls_wrap.js:758:25)
I am using windows OS. But why this error? How should I fix this?
So I am new to express and io but I had a server running fine for webRTC but now there is a deprecated method in webRTC that only runs on https so I tried to create an https server but it starts and then immediately exits. I cannot figure out what is wrong and I do not get any errors. Also I am using an aws ec2 to run the express io server. Maybe someone can spot where in my syntax/implementation I am going wrong.
Note I have been googling around for the past half hour and cannot figure it out
Here is the code:
var connect = require('connect');
var https = require('https');
var fs = require('fs');
var express = require('express.io');
var app = express();
//app.http().io();
var PORT = 443;
var options = {
key: fs.readFileSync('../server.key'),
cert: fs.readFileSync('../server.crt')
};
app.https(options).io();
//var app = https.createServer(options, app1);
console.log('server started on port ' + PORT);
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.render('index.ejs');
});
app.listen(PORT);
app.io.route('ready', function(req) {
req.io.join(req.data.chat_room);
req.io.join(req.data.signal_room);
app.io.room(req.data).broadcast('announce', {
message: 'New client in the ' + req.data + ' room.'
})
})
Update
I am putting a bounty on this because I would like someone to provide me with a complete answer on setting up the server for production.
You need to add a rule for port 443 in a Security Group for your instance.
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html might help.