跳至正文

本地搭建支持https的Web服务器

首先需要有Node.js环境。

1 安装 http-server

打开命令行窗口,进入项目目录,通过 npm 安装 http-server

cd project
npm install -g http-server

2 创建证书

通过 openssl 创建私钥和证书:

openssl genrsa 2048 > key.pem
openssl req -x509 -days 1000 -new -key key.pem -out cert.pem

3 启动Web服务

私钥和证书都有了后,就可以通过 SSL 在本地启动 Web服务了。

http-server --ssl -c-1 -p 8080 -a 127.0.0.1
或
http-server --ssl

以下提示代表成功。

Available on:
  https://127.0.0.1:8080

最后,如果项目目录下有index.html,则输入类似下面的地址,就OK了。

https://127.0.0.1:8080/index.html

欢迎关注微信公众号“楚游香”,获取更多文章和交流。

标签:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注