当前位置: 首页 > news >正文

【VSCode】在 VSCode 中运行 HTML 页面并通过 HTTPS 访问

在 Visual Studio Code 中运行 HTML 页面并通过 HTTPS 访问,通常需要借助本地开发服务器并配置 SSL 证书。以下是几种常见方法:


方法 1:使用 Live Server 扩展 + 自签名证书

步骤 1:生成自签名证书
  1. 使用 OpenSSL 生成证书(需提前安装 OpenSSL):

    openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
    
    • 按提示填写信息(可直接回车跳过)。
    • 生成 key.pem(私钥)和 cert.pem(证书)文件。
  2. 将生成的证书文件(如 key.pemcert.pem)放在项目目录中。

步骤 2:配置 Live Server
  1. 安装 Live Server 扩展。
  2. 打开 VS Code 设置(Ctrl + ,),搜索 Live Server,找到以下设置:
    • Live Server > Settings: Https:启用(设为 true)。
    • Live Server > Settings: Cert:填写证书路径(如 cert.pem)。
    • Live Server > Settings: Key:填写私钥路径(如 key.pem)。
  3. 右键 HTML 文件,选择 Open with Live Server,浏览器将通过 https://localhost:5500 访问。

方法 2:使用 Node.js 快速搭建 HTTPS 服务器

步骤 1:创建服务器脚本
  1. 新建文件 server.js,写入以下代码:
    const https = require('https');
    const fs = require('fs');
    const path = require('path');const options = {key: fs.readFileSync('key.pem'),    // 私钥路径cert: fs.readFileSync('cert.pem')   // 证书路径
    };const server = https.createServer(options, (req, res) => {const filePath = path.join(__dirname, req.url === '/' ? 'index.html' : req.url);fs.readFile(filePath, (err, data) => {if (err) {res.writeHead(404);res.end('File not found');} else {res.writeHead(200);res.end(data);}});
    });server.listen(3000, () => {console.log('HTTPS server running on https://localhost:3000');
    });
    
步骤 2:运行服务器
  1. 确保已生成 key.pemcert.pem(方法同上)。
  2. 在终端中执行:
    node server.js
    
  3. 浏览器访问 https://localhost:3000

方法 3:使用 http-server(推荐)

步骤 1:安装 http-server
npm install -g http-server
步骤 2:生成可信本地证书(推荐使用 mkcert)
  1. 安装 mkcert(更安全的本地证书):
    # Windows (Chocolatey)
    choco install mkcert# macOS (Homebrew)
    brew install mkcert# Linux
    sudo apt install mkcert
    
  2. 生成并信任证书:
    mkcert -install
    mkcert localhost
    
    • 生成 localhost.pem(证书)和 localhost-key.pem(私钥)。
步骤 3:启动 HTTPS 服务器
http-server --ssl --cert localhost.pem --key localhost-key.pem -p 8080
  • 访问 https://localhost:8080

方法 4:使用 Webpack-dev-server(适用于前端工程化项目)

  1. webpack.config.js 中配置:
    module.exports = {// ...devServer: {https: {key: fs.readFileSync('key.pem'),cert: fs.readFileSync('cert.pem')},port: 8080}
    };
    
  2. 运行 webpack serve

解决浏览器安全警告

  • 自签名证书:浏览器会提示“不安全”,可手动信任(Chrome 中输入 thisisunsafe 快速绕过)。
  • mkcert 证书:已自动信任,无警告。

选择最适合你的方法,快速在本地启用 HTTPS 开发环境!

http://www.xdnf.cn/news/6427.html

相关文章:

  • 在pycharm中搭建yolo11分类检测系统--PyQt5学习(二)
  • 发现“横”字手写有难度,对比两个“横”字
  • CSS3笔记
  • 小知识合集 慢慢更新
  • vue,uniapp解决h5跨域问题
  • uniapp打包IOS私钥证书过期了,如何在非mac系统操作
  • PDK中technology file从tf格式转换为lef格式
  • 【AI插件开发】Notepad++ AI插件开发实践:支持配置界面
  • 双轮驱动能源革命:能源互联网与分布式能源赋能工厂能效跃迁
  • 在Ubuntu系统中安装和升级RabbitVCS
  • 【教程】无视硬件限制强制升级Windows 11
  • 《数据结构之美--链表oj练习》
  • 2026《数据结构》考研复习笔记三(C++高级教程)
  • 「数据可视化 D3系列」入门第十章:饼图绘制详解与实现
  • 《实战AI智能体》——邮件转工单的AI自动化
  • 「数据可视化 D3系列」入门第十一章:力导向图深度解析与实现
  • 设计模式 --- 装饰器模式
  • 通过 Zotero 的样式编辑器(Style Editor)自定义文献引用和参考文献列表的格式
  • 在阿里云虚拟主机上启用WordPress伪静态
  • Redis 的指令执行方式:Pipeline、事务与 Lua 脚本的对比
  • HTTP:九.WEB机器人
  • 探索 HumanoidBench:类人机器人学习的新平台
  • 甘果桌面tv版下载-甘果桌面安卓电视版使用教程
  • OpenAI 34页最佳构建Agent实践
  • Python(23)Python异常处理完全指南:从防御到调试的工程实践
  • 使用 Vue 开发登录页面的完整指南
  • 解决 Spring Boot 多数据源环境下事务管理器冲突问题(非Neo4j请求标记了 @Transactional 尝试启动Neo4j的事务管理器)
  • 数据库原理及应用mysql版陈业斌实验四
  • 若依同步企业微信架构及ACTIVITI
  • docker部署springboot(eureka server)项目