快速搭建 HTTP 静态服务的 10 种方法

工具利器 Sep 25, 2020

作为程序员,需要经常起一个静态服务器(http static server,在命令执行的所在路径启动一个 http 服务器,即可通过浏览器访问该路径下的所有文件),来完成一些任务,如局域网内传文件,或者测试网页等;本文主旨即分享,如何快速搭建 HTTP 静态服务的 10 种方法,对于非程序员人也可以使用。

快速搭建 HTTP 静态服务的 10 种方法

Python

基于 Python 2.x

python -m SimpleHTTPServer 8000
# Serving HTTP on 0.0.0.0 port 8000 ...

基于 Python 3.x

python -m http.server 8000

基于 Livereload 库

pip install livereload
#serves current dir with port 35729 which livereload extensions use
livereload
# 指定端口和路径      
livereload ~/testing -p 8000

Node.js

基于 arya-jarvis

# 安装依赖
npm i arya-jarvis -g

# 默认 8080 端口,如被占用,自增长
arya server
# 👏 Or Use Alias
arya s

或者直接使用 npx 来完成✅:

# 安装 + 使用
npx arya-jarvis server

基于 http-server

# 安装依赖
npm install -g http-server
http-server -p 8000

# 或者基于 npx 
npx http-server

基于 anywhere

# 安装依赖
npm install -g anywhere

anywhere
# 自定义端口
anywhere -p 8000

# 或者基于 npx 
npx anywhere -p 8000

基于 node-static

# 安装依赖
npm install -g node-static 
static -p 8000

# 或者基于 npx 
npx node-static -p 8000

Go

基于 spark

go get github.com/rif/spark
spark -port 8000 .

PHP

基于 PHP (>= 5.4)

php -S 127.0.0.1:8000

Ruby

基于 Ruby 1.9.2+

ruby -run -ehttpd . -p8000

如果您使用 Mac 或 Linux 操作系统,可以在 .zshrc 配置中添加如下这句,即可在终端,使用 server 命令开启静态服务(当然,您将方式替换成如上提到的方法也可以):

alias server="ruby -run -ehttpd . -p 8080"

基于 serve 库

# 安装工具库
gem install serve
serve

Golang

# 安装工具库
go get github.com/vwochnik/gost
gost

基于 curl

curl lama.sh | sh

Tags

轩帅

来自陕南一隅;现栖身于深圳福田,作为一介程序员。略崇文喜武,大爱豪杰美人,也尚科技。

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.