nodejs创建https服务
来源:程序员人生 发布时间:2014-12-07 09:47:46 阅读次数:3314次
nodejs创建https服务
by 伍雪颖
serverr.js
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('./keys/server.key'),
cert: fs.readFileSync('./keys/server.crt'),
};
https.createServer(options,function(req,res) {
res.writeHead(200);
res.end("hello world!
");
}).listen(8000);
client.js
var https = require('https');
var fs = require('fs');
var options = {
hostname: 'localhost',
port:8000,
path:'/',
method:'GET',
key:fs.readFileSync('./keys/client.key'),
cert:fs.readFileSync('./keys/client.crt'),
ca:[fs.readFileSync('./keys/ca.crt')],
rejectUnauthorized:false
};
options.agent = new https.Agent(options);
var req = https.request(options,function(res) {
res.setEncoding('utf⑻');
res.on('data',function(d) {
console.log(d);
});
});
req.end();
req.on('error',function(e) {
console.log(e);
});
证书生成参照上1章:nodejs创建TLS服务
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠