nodejs实现https服务器

2022-03-07  本文已影响0人  一路向后

1.源码实现

var https = require("https");
var fs=require("fs");

var options = {
    key: fs.readFileSync('./rsa_private.key'),
    cert: fs.readFileSync('./rsa_cert.crt')
};

https.createServer(options, function(req,res){
    res.writeHead(200, {
        "content-type":"text/plain"
    });
    res.write("hello nodejs\n");
    res.end();
}).listen(8080);

2.生成证书

$ openssl genrsa -out rsa_private.key 1024
$ openssl req -new -key rsa_privat.key -out rsa_certrequest.csr
$ openssl x509 -req -in rsa_certrequest.csr -signkey rsa_private.key -out rsa_cert.crt

3.运行及其结果

$ node test.js
$ curl -k https://localhost:8080
hello nodejs
上一篇下一篇

猜你喜欢

热点阅读