文件服务器示例
本项目演示了如何使用 Fiber 框架在 Go 应用中设置一个简单的文件服务器。
前提条件
确保您已安装以下内容
- Go 语言
- Fiber 包
设置
-
克隆仓库
git clone https://github.com/gofiber/recipes.git
cd recipes/file-server -
安装依赖项
go get
运行应用
-
启动应用
go run main.go
-
访问文件服务器,地址为
http://localhost:3000
。
示例
以下是用于提供静态文件的 Fiber 应用的 main.go
示例文件
package main
import (
"log"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
// Serve static files from the "public" directory
app.Static("/", "./public")
log.Fatal(app.Listen(":3000"))
}