nodejs创建https服务

页面导航:首页 > 网络编程 > JavaScript > nodejs创建https服务

nodejs创建https服务

来源: 作者: 时间:2016-02-05 11:06 【

nodejs创建https服务by 伍雪颖serverr jsvar https = require( 39;https 39;);var fs = require( 39;fs 39;);var options = {key: fs readFileSync( 39; keys server key 39;),cert

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

相关文章

    文章评论

    最 近 更 新
    热 点 排 行
    Js与CSS工具
    代码转换工具
    
    <