node js ssl证书问题

时间:2015-12-25 13:32:35

标签: node.js ssl express

当我在Node.JS v0.10.36上运行如下代码时 - 服务器响应https请求,但后来我在Node.JS v4.2.1上运行相同的代码 - 服务器根本没有响应,但是时间浏览器不会“说”出现任何问题 - 它只是继续加载页面。我应该以某种方式重写代码吗?

var express = require('express');
var https = require('https');
//var http = require('http');
var fs = require('fs');
var crypto=require('crypto')
var app = express();
var ssl_conf=require(__dirname+'/config/ssl.json');

var secureContext = {}

try{
    for(var domain in ssl_conf){
        secureContext[domain]=getSecureContext(ssl_conf[domain].domain);
    }
}
catch(err){
    console.log('error with ssl.config file '+err);

}

function getSecureContext (domain) {//returns secure context
    return crypto.createCredentials({
        key:  fs.readFileSync('./ssl/'+domain+'.key'),
        cert: fs.readFileSync('./ssl/'+domain+'.crt')
      }).context;
}

var options = {
    SNICallback: function (domain) {
        return secureContext[domain];
    },
//in case SNI is not available use this cert 
    cert: fs.readFileSync('./ssl/2_helena.softpro.ua.crt'),
    key: fs.readFileSync('./ssl/2_helena.softpro.ua.key')
    }

app.get('/',function(req,res){//simple route
    res.send("your domain is "+req.hostname);
})

https.createServer(options, app).listen(443,function(){//run server
    console.log('https server running on 443')  
});

1 个答案:

答案 0 :(得分:0)

在最新版本12.0.0 +下一个代码没问题

var options = {
    SNICallback:function(domain,cb){
        var ctx= tls.createSecureContext(
                secureContext[domain]//{key:<Buffer>,cert:<Buffer>}
            ).context   
        if(cb)
            cb(null,ctx)
        else
            return ctx;
    }
}
相关问题