学习前端开发,想要调试静态页面以及js,发现直接打开本地会有跨域异常,因此需要启动一个静态服务器,只负责当前目录的文件路由,
目前尝试两种方案:
nginxhttp-serverbrew install nginxMac通过brew安装后的配置文件位于:/usr/local/etc/nginx.
修改端口和文件目录:
server {
listen 8090;
server_name server.com;
charset utf-8;
location / {
alias /Users/wangyaxing/test/;
index index.html;
}
....
}# 启动
nginx
# 关闭
nginx -s stop启动后,打开浏览器,输入: localhost:8090/xxx.html即可。
http-server[1]是基于node.js的HTTP 服务器,它最大的好处就是:可以使用任意一个目录成为服务器的目录,完全抛开后端的沉重工程,直接运行想要的js代码
npm install -g http-serverhttp-server就可以以 该目录为根目录启动一个服务器
http-server [path] [options]path是目录的路径名称public 目录,默认为 ./public;public 目录,那么就是 根目录 ./options常用选项-p 或者 --port 使用的端口号,默认为 8080-a 使用的 IP 地址,默认0.0.0.0options更多选项,可以查看http-server[2]
cd test/
http-server -p 8900你可以把 http-server -p 8900 写入到 package.json 文件中的 scripts 字段中
"scripts": {
"dev": "http-server -p 8900"
},[1]http-server: https://github.com/indexzero/http-server
[2]http-server: https://github.com/indexzero/http-server#available-options