程序员人生 网站导航

nodejs创建https服务

栏目:综合技术时间:2014-12-07 09:47:46

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服务
------分隔线----------------------------
------分隔线----------------------------

最新技术推荐