Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何启动一个本地静态服务器 #72

Open
funnycoderstar opened this issue Jun 17, 2019 · 0 comments
Open

如何启动一个本地静态服务器 #72

funnycoderstar opened this issue Jun 17, 2019 · 0 comments
Labels

Comments

@funnycoderstar
Copy link
Owner

funnycoderstar commented Jun 17, 2019

如何启动一个本地静态服务器

背景:学习前端开发,想要调试静态页面以及js,发现直接打开本地会有跨域异常,因此需要启动一个静态服务器,只负责当前目录的文件路由,

目前尝试两种方案:

  1. nginx
  2. http-server

nginx

安装

brew install nginx

配置

Mac通过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

http-server是基于node.js的HTTP 服务器,它最大的好处就是:
可以使用任意一个目录成为服务器的目录,完全抛开后端的沉重工程,直接运行想要的js代码

安装

npm install -g http-server

启动

http-server就可以以 该目录为跟启动一个服务器

http-server [path] [options] 
  • path是目录的路径名称
    • 如果存在public目录,默认为./public;
    • 如果没有 public 目录,那么就是 根目录 ./
  • options常用选项
    • -p 或者 --port 使用的端口号,默认为8080
    • -a 使用的IP地址,默认0.0.0.0

options更多选项,可以查看http-server

使用

cd test/
http-server  -p 8900

你可以把 http-server -p 8900 写入到 package.json 文件中的 scripts 节点

"scripts": {
    "dev": "http-server  -p 8900"
  },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant