http-server在本地启动https服务以及配置域名

网上搜了一遍后,才发现,看官方文档才是最直接最准确最快速的做法。

1、安装http-server

npm install --global http-server

2、生成证书文件,有两个。一个是cert.pem, 一个是key.pem 

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem

这里成功的文件可以修改地址,最后在启动的时候 -C 后面跟cert的路径,-K 后面跟key的路径,注意填绝对路径,如/usr/local/etc/nginx/ssl/cert.pem。路径在启动服务的时候对应上就行。默认是生成的当前目录的。

 3、启动服务。启动服务的时候,我的是前端项目,所以会进入到dist目录,里面有index.html的文件夹,运行命令。

http-server -S -C cert.pem

注意大小写,这里指定目录的时候是大写;用上面代码执行的时候你的两个证书文件都要放在当前目录下,否则的话就要写明证书文件的路径。

# 这里是不写 —C 或者 —K 的时候的默认值:
-C or --cert ssl cert 文件路径 (default: cert.pem) -K or --key Path to ssl key file (default: key.pem)

 看到下面的效果就是启动成功了。

Starting up http-server, serving ./ through https
Available on:
  https://127.0.0.1:8080
  https://10.0.51.99:8080
Hit CTRL-C to stop the server

4、配置域名。这一步其实和http-server没有关系,因为我用到也提一下。在某些设定下可以解决后端请求有域名要求,域名限制的问题。

配置其实很简单,就是利用host,以为http-server都是在 127.0.0.1 上启动服务的,我们利用host转发过去就好了。

127.0.0.1 test.jd.com

配置上述host就可以在浏览器实现 https://test.jd.com:8080 来访问了。

 

注意:在最新的谷歌浏览器中,会出现拦截并无法访问的情况。应该是谷歌浏览器安全又升级了,这个换成火狐浏览器,选择高级,接受风险并继续就可以了。根据经验,谷歌浏览器通过忽略安全的方式启动应该也可以,有时间查一下补上吧。

 

 

http-server 参数说明:

-p 端口号 (默认 8080)
-a IP 地址 (默认 0.0.0.0)
-d 显示目录列表 (默认 'True')
-i 显示 autoIndex (默认 'True')
-e or --ext 如果没有提供默认的文件扩展名(默认 'html')
-s or --silent 禁止日志信息输出
--cors 启用 CORS via the Access-Control-Allow-Origin header
-o 在开始服务后打开浏览器
-c 为 cache-control max-age header 设置Cache time(秒) , e.g. -c10 for 10 seconds (defaults to '3600'). 禁用 caching, 则使用 -c-1.
-U 或 --utc 使用UTC time 格式化log消息
-P or --proxy Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com
-S or --ssl 启用 https
-C or --cert ssl cert 文件路径 (default: cert.pem)
-K or --key Path to ssl key file (default: key.pem).
-r or --robots Provide a /robots.txt (whose content defaults to 'User-agent: *\nDisallow: /')

 

 

附上原文(https://www.npmjs.com/package/http-server)。

TLS/SSL

First, you need to make sure that openssl is installed correctly, and you have key.pem and cert.pem files. You can generate them using this command:

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem

You will be prompted with a few questions after entering the command. Use 127.0.0.1 as value for Common name if you want to be able to install the certificate in your OS's root certificate store or browser so that it is trusted.

This generates a cert-key pair and it will be valid for 3650 days (about 10 years).

Then you need to run the server with -S for enabling SSL and -C for your certificate file.

http-server -S -C cert.pem

If you wish to use a passphrase with your private key you can include one in the openssl command via the -passout parameter (using password of foobar)

e.g. openssl req -newkey rsa:2048 -passout pass:foobar -keyout key.pem -x509 -days 365 -out cert.pem

For security reasons, the passphrase will only be read from the NODE_HTTP_SERVER_SSL_PASSPHRASE environment variable.

This is what should be output if successful:

Starting up http-server, serving ./ through https

http-server settings:
CORS: disabled
Cache: 3600 seconds
Connection Timeout: 120 seconds
Directory Listings: visible
AutoIndex: visible
Serve GZIP Files: false
Serve Brotli Files: false
Default File Extension: none

Available on:
  https://127.0.0.1:8080
  https://192.168.1.101:8080
  https://192.168.1.104:8080
Hit CTRL-C to stop the server
posted @ 2022-07-11 21:35  佳明兄  阅读(3971)  评论(0编辑  收藏  举报

内容仅为参考使用,不保证内容的正确性,通过使用本博客内容随之而来的风险与作者无关。内容如有侵权,请通知删除!