I'm trying to start the server in HTTPS using Express 4.x. I tried
var credentials = {key: privateKey, cert: certificate};
var app = express(credentials);
But it's not working.
You could easily just do something like this:
var app = express(),
credentials = {key: privateKey, cert: certificate},
server = https.createServer(credentials, app);
server.listen(8443);
Related
I'm running a https example server on node.js with express.
I have my certificates and I'm able to run it on port 3000 or 8443.
But I have to put the port on the domain mydomine.com:8443 and if I want to access the https without saying the port, that should be 443, but this port doesn't load.
I even tried changing the ports on my router to point to a working port, such as ext443->in8443, but that didn't work either. (ports configuration)
So I'm wondering if it's really 443 that I'm looking for?
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync(__dirname + 'ssl/privkey.pem', 'utf8');
var certificate = fs.readFileSync(__dirname + 'ssl/fullchain.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
var httpsServer = https.createServer(credentials, app);
app.use(express.static('public'));
httpsServer.listen(8443);
i even trdied mydomine.com:443 and doent work either
443 requires sudo on linux, and admin permissions on Windows, because it's reserved. You can run this program as sudo.
If it didn't helpful, please, attach screenshot
I have a problem since i activated letsencrypt on my domain and did'nt have problem with http server before.
Here is my app.js code:
var app = require('express')();
var fs = require('fs');
var https = require('https');
var secureServer = https.createServer({
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.cert'),
ca: fs.readFileSync('server.cacert'),
requestCert: true,
rejectUnauthorized: false
}, app).listen(5221, function() {
console.log("Secure Express server listening on port "+ 5221);
});
var io = require('socket.io')(secureServer);
The Secure Express server listening on port 5221 prints out but nothing more and the codes in:
io.on('connection', function (socket) {
console.log(`Socket ${socket.id} connected.`);
}
Is not working at all.
I've also tested with .pem files, with ca.crt or without that... but nothing changes.
I have a NodeJs Application running with express. I have been trying to integrate an SSL certificate but I keep having the same error: Error: error:0906D06C:PEM routines:PEM_read_bio:no start line.
I have tried some solutions I found on stackOverflow like
var privateKey = fs.readFileSync( 'privatekey.pem' );
var certificate = fs.readFileSync( 'certificate.pem' );
https.createServer({
key: privateKey,
cert: certificate
}, app).listen(port);
or
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync('privatekey.pem');
var certificate = fs.readFileSync('certificate.pem');
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
httpServer.listen(8081);
httpsServer.listen(8443);
But none seem to work. My main problem is that I donwloaded my certificate file from GoDaddy but I only get two .cert files. I have read in some websites how to obtain the .pem files but I keep getting the same error.
As you can see I am new at using https protocol in NodeJs.
Could you help me solve this problem?
Best regards
I am trying to get both ws and wss working, on one WebSocket server.
This is what I currently have, only wss
var WebSocket = require('ws');
var https = require('https');
var privateKey = fs.readFileSync('cert/key.key');
var certificate = fs.readFileSync('cert/cert.crt');
var httpsServer = https.createServer({
ca: ca,
key: privateKey,
cert: certificate
}, this.app);
var options = {
server: httpsServer,
perMessageDeflate: false,
maxPayload: 4096
};
var wss = new WebSocket.Server(options);
I know using Socket.io it is really easy, you can just do io.attach(httpServer) and io.attach(httpsServer).
var httpServer = http.createServer(this.app);
var httpsServer = https.createServer({
key: privateKey,
cert: certificate
}, this.app);
httpServer.listen(3002, function(){
console.log('httpServer listening on port 3002');
});
httpsServer.listen(3003, function(){
console.log('httpsServer listening on port 3003');
});
this.io = new ioServer();
this.io.attach(httpServer);
this.io.attach(httpsServer);
Is this even possible, using this WebSocket library?
I have found that snippet that works fine:
var ssl = {
key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/cert.pem')
};
var httpsServer = https.createServer(ssl).listen(port);
However my site uses that server construction:
var server = http.createServer(httpRequestsHandler).listen(_port_, function() {
process.setgid('www-data');
process.setuid('user1');
});
Question: how/where do I specify the ssl in this case ?
Based on the documentation your code has to look like this:
const https = require('https');
const fs = require('fs');
var ssl = {
key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/cert.pem')
};
var server = https.createServer(ssl, httpRequestsHandler).listen(_port_, function() {
process.setgid('www-data');
process.setuid('user1');
});
I recommend that you look at this site