码迷,mamicode.com
首页 > Web开发 > 详细

nodejs创建https服务

时间:2014-11-30 11:26:20      阅读:193      评论:0      收藏:0      [点我收藏+]

标签: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服务

nodejs创建https服务

标签:nodejs   https   

原文地址:http://blog.csdn.net/rainlesvio/article/details/41620897

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!