I have a problem with https in express and I don't really understand it:
This is from my last project (it works):
index.js:
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync(__dirname + '/cert/server.key', 'utf8');
var certificate = fs.readFileSync(__dirname + '/cert/server.cert', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
httpServer.listen(80);
httpsServer.listen(443);
//Force https
app.use(function(req, res, next) {
if(!req.secure) {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
next();
});
This is from my current project (doesn't work):
index.js:
var fs = require('fs');
var configRaw = fs.readFileSync(__dirname + '/config.json', 'utf8', function(err, data) {
if(err) {
return abort('Can not read/ find' + __dirname + '/config.json'); //FIXME
}
});
var config = JSON.parse(configRaw);
console.log(' - loading webserver script..');
var webServer = require(__dirname + '/lib/web_server.js');
webServer.init(config.httpPort, config.httpsPort);
web_server.js:
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync(__dirname + '/../ssl/server.key', 'utf8');
var certificate = fs.readFileSync(__dirname + '/../ssl/server.cert', 'utf8');
var credential = { key: privateKey, vert: certificate };
var express = require('express');
var app = express();
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credential, app);
this.init = function(httpPort, httpsPort){
httpServer.listen(httpPort);
httpsServer.listen(httpsPort);
}
app.use(function(req, res, next) {
if(!req.secure) {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
next();
});
I am using the same certificate and key as before and never got an fs-error while reading a file or something.
But when I go to localhost via the browser I get this message: ERR_SSL_VERSION_OR_CIPHER_MISMATCH.
Please keep in mind that I am fairly new to Node.js - thank you very much in advance! ;)
Looks like you've got a typo:
var credential = { key: privateKey, vert: certificate };
"vert" instead of "cert" :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With