1.安装mkert 需要超级管理员权限安装

@”%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe” -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command “iex ((New-Object System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’))” && SET “PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin”

choco
choco install mkcert
mkcert -install
win+ R 输入 certmgr.msc
受信任的根证书颁发机构->证书
C:\Users\antony\AppData\Local\mkcert
mkcert localhost 127.0.0.1 ::1
通过输出,我们可以看到成功生成了localhost+2.pem证书文件和localhost+2-key.pem私钥文件,只要在web server上使用这两个文件就可以了。
https://www.jianshu.com/p/7cb5c2cffaaa
https://blog.csdn.net/qq_39586877/article/details/126268564

//导入模块是require
const http = require('http')
const fs = require('fs')
const path = require('path')
const https = require('https')


//1.创建一个http服务
//2.监听一个端口8888
//3.启动运行服务 node httpserver.js

// http.createServer(function(request,response){
    /*
    //浏览器怎么认出helloserver
    response.writeHead(200,{'Content-type': 'text/html'})
    //给浏览器输出内容
    response.end("<strong>hello server!!!</strong>")
    */

//    if(request.url === '/' || request.url === '/index'){

//     filePath = path.join(__dirname,'template','index.html')
//     fs.readFile(filePath,function(err,data){
//      if(err){
//          throw err;
//      }else{
//          response.end(data)
//      }
//     })

//    }

//   else if(request.url === '/login'){

//     filePath = path.join(__dirname,'template','login.html')
//     fs.readFile(filePath,function(err,data){
//      if(err){
//          throw err;
//      }else{
//          response.end(data)
//      }
//     })

//    }


//   else if(request.url === '/register'){

//     filePath = path.join(__dirname,'template','register.html')
//     fs.readFile(filePath,function(err,data){
//      if(err){
//          throw err;
//      }else{
//          response.end(data)
//      }
//     })

//    }

//    else{

//     filePath = path.join(__dirname,'template','404.html')
//     fs.readFile(filePath,function(err,data){
//      if(err){
//          throw err;
//      }else{
//          response.end(data)
//      }
//     })

//    }

// }).listen(8888)
// console.log("你启动的服务是http://127.0.0.1:8888")

const privateKey = fs.readFileSync(path.join(__dirname,'./localhost+2-key.pem'),'utf8')
const certificate = fs.readFileSync(path.join(__dirname,'./localhost+2.pem'),'utf8')
const credentials = {
    key: privateKey,
    cert: certificate
}


https.createServer(credentials,function(request,response){
    /*
    //浏览器怎么认出helloserver
    response.writeHead(200,{'Content-type': 'text/html'})
    //给浏览器输出内容
    response.end("<strong>hello server!!!</strong>")
    */

   if(request.url === '/' || request.url === '/index'){

    filePath = path.join(__dirname,'template','index.html')
    fs.readFile(filePath,function(err,data){
     if(err){
         throw err;
     }else{
         response.end(data)
     }
    })

   }

  else if(request.url === '/login'){

    filePath = path.join(__dirname,'template','login.html')
    fs.readFile(filePath,function(err,data){
     if(err){
         throw err;
     }else{
         response.end(data)
     }
    })

   }


  else if(request.url === '/register'){

    filePath = path.join(__dirname,'template','register.html')
    fs.readFile(filePath,function(err,data){
     if(err){
         throw err;
     }else{
         response.end(data)
     }
    })

   }

   else{

    filePath = path.join(__dirname,'template','404.html')
    fs.readFile(filePath,function(err,data){
     if(err){
         throw err;
     }else{
         response.end(data)
     }
    })

   }

}).listen(8888)
console.log("你启动的服务是https://127.0.0.1:8888")