一个原生的静态html项目需要前后端交互或测试的时候怎么办呢?(在本地开启http服务)

我们做项目的时候有时用webpack ,glup,什么的都会生成一个本地地址,但是原生的没有,这时候当你需要前后端交互,或者测试说我们连你IP在你本地先测吧!这时候该怎么办呢,不可能临时换成vue,或者其他的没有必要。于是问了一下别人叫了我一个很使用的方法!

要像使用这个方法前提是你已经安装了node.js。

http-server 是一个简单的零配置命令行HTTP服务器, 基于 nodeJs.

安装 (全局安装加 -g) : 

 npm install http-server 

Windows 下使用:

在站点目录下开启命令行输入

 http-server

访问: http://localhost:8080 or http://127.0.0.1:8080 

用法:

http-server [path] [options]

[path]默认情况下,./public如果文件夹存在,./否则。(一般直接在项目中输入http-server就可以了,)

扫描二维码关注公众号,回复: 3996453 查看本文章

现在您可以访问http:// localhost:8080来查看您的服务器

参数 :

-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: /')

-h or --help 打印以上列表并退出 

猜你喜欢

转载自blog.csdn.net/fengxiaopeng74/article/details/81698232