nodejs创建https服务

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!\n");
}).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-8');
	res.on('data',function(d) {
		console.log(d);
	});
});
req.end();

req.on('error',function(e) {
	console.log(e);
});

证书生成参照上一章:nodejs创建TLS服务

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。