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

简介: 背景学习前端开发,想要调试静态页面以及js,发现直接本地打开会有跨域异常。因此,需要启动一个静态服务器,只负责当前目录的文件路由。目前尝试了两种方式。一种是Nginx, 一种是NodeJS服务器。最终推荐NodeJS。

背景

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

目前尝试了两种方式。一种是Nginx, 一种是NodeJS服务器。最终推荐NodeJS。

1. Nginx启动静态页面

1.1. 安装

下载安装Nginx,在Mac下安装如下:

brew install nginx

1.2 配置

Mac通过brew安装后的配置文件位于:/usr/local/etc/nginx.

修改端口和文件目录:

server {
        listen       8070;
        server_name  localhost;

        charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Users/ryan/workspace/learning/react;
            index  index.html index.htm;
        }
....
}

1.3 启动和关闭

启动

nginx

关闭

nginx -s stop

启动后,打开浏览器,输入:localhost:8070/xxx.html即可。

2. 通过NodeJS的本地服务器

本次选用http-server

2.1 安装

npm install http-server -g

2.2 启动

http-server [path] [options]

Options

-p Port to use (defaults to 8080)

-a Address to use (defaults to 0.0.0.0)

-d Show directory listings (defaults to 'True')

-i Display autoIndex (defaults to 'True')

-g or --gzip When enabled (defaults to 'False') it will serve ./public/some-file.js.gz in place of ./public/some-file.js when a gzipped version of the file exists and the request accepts gzip encoding.

-e or --ext Default file extension if none supplied (defaults to 'html')

-s or --silent Suppress log messages from output

--cors Enable CORS via the Access-Control-Allow-Origin header

-o Open browser window after starting the server

-c Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds (defaults to '3600'). To disable caching, use -c-1.

-U or --utc Use UTC time format in log messages.

-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 Enable https.

-C or --cert Path to ssl cert file (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 Print this list and exit.

其他

直接使用IDEA或者webstom的用浏览器打开功能就可以了。





唯有不断学习方能改变! -- Ryan Miao
目录
相关文章
|
6月前
|
前端开发 应用服务中间件 nginx
网页设计,若依项目修改(It must be done)01----若依打包位置,nginx代理前端静态资源和后端接口,就是怎样设置转载,访问固定端口,让他访问其他资料的配置文件,访问/,给你那些
网页设计,若依项目修改(It must be done)01----若依打包位置,nginx代理前端静态资源和后端接口,就是怎样设置转载,访问固定端口,让他访问其他资料的配置文件,访问/,给你那些
|
7月前
|
Windows
windows系统bat批处理 网络设置大全 设置静态、动态IP地址
windows系统bat批处理 网络设置大全 设置静态、动态IP地址
1440 2
|
8月前
|
数据挖掘 Python
如何将动态IP代理转换为静态IP代理的方法与步骤
如何将动态IP代理转换为静态IP代理的方法与步骤
459 6
|
8月前
|
Python
如何动态切换代理IP
如何动态切换代理IP
|
8月前
|
数据采集 安全 大数据
使用代理IP时有哪些小技巧?
代理IP工具在大数据和跨境行业广泛使用,能隐藏真实IP并提升数据采集效率。选择时考虑代理IP的质量、速度、稳定性和价格,确保服务商信誉安全。测试多个代理IP以满足不同需求,设置正确请求头信息避免被目标服务器屏蔽。避免频繁更换地区,定期更新代理IP,并保护个人信息。根据业务需求制定使用计划,提前学习相关技巧,可避免后期问题。
|
网络协议 开发工具 git
命令终端设置全局代理的两种方法
命令终端设置全局代理的两种方法
465 0
|
应用服务中间件 Shell 程序员
启动 Tomcat 应用服务器端口 8080 被占用排查思路及解决方式
启动 Tomcat 应用服务器端口 8080 被占用排查思路及解决方式
628 0
|
Java 应用服务中间件 Spring
tomcat配置多域名站点启动时项目重复加载多次
在tomcat中配置多个Host的时候, 出现项目重复启动多次的情况. 刚开始以为是spring boot发布项目的时候自带了一个tomcat引起的, 后来发现不是
211 0
tomcat配置多域名站点启动时项目重复加载多次
BAT 脚本技术-利用bat批处理脚本静态指定ip地址、自动获取ip地址设置
BAT 脚本技术-利用bat批处理脚本静态指定ip地址、自动获取ip地址设置
1041 0
BAT 脚本技术-利用bat批处理脚本静态指定ip地址、自动获取ip地址设置