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服务