美文网首页
nodejs实现https服务器

nodejs实现https服务器

作者: 一路向后 | 来源:发表于2022-03-07 22:01 被阅读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

相关文章

网友评论

      本文标题:nodejs实现https服务器

      本文链接:https://www.haomeiwen.com/subject/thcorrtx.html