静态文件服务
简介
HttpServer 内置静态文件服务功能,当配置 WebRoot 后,会自动处理静态资源请求,无需编写控制器。
快速配置
vb
Private Sub Form_Load()
Set Server = New cHttpServer
' 配置静态文件根目录
Server.Start 8080, "C:\WebRoot"
Debug.Print "静态文件服务: http://localhost:8080/"
End Sub目录结构示例
C:\WebRoot\
├── index.html # 首页
├── favicon.ico # 网站图标
├── css\
│ ├── style.css
│ └── theme.css
├── js\
│ ├── app.js
│ └── utils.js
├── images\
│ ├── logo.png
│ └── banner.jpg
└── upload\
└── avatar.png请求映射
| 请求 URL | 映射到文件 |
|---|---|
/ | C:\WebRoot\index.html |
/css/style.css | C:\WebRoot\css\style.css |
/js/app.js | C:\WebRoot\js\app.js |
/images/logo.png | C:\WebRoot\images\logo.png |
支持的 MIME 类型
vb
' 系统自动识别以下文件类型的 Content-Type
' 文本类型
text/html -> .html, .htm
text/css -> .css
text/javascript -> .js
text/plain -> .txt
' 图片类型
image/png -> .png
image/jpeg -> .jpg, .jpeg
gif/image -> .gif
image/svg+xml -> .svg
image/x-icon -> .ico
' 应用类型
application/json -> .json
application/xml -> .xml
application/pdf -> .pdf
' 字体类型
font/woff2 -> .woff2
font/woff -> .woff优先级说明
静态文件优先于路由匹配:
请求 /index.html
│
├──> 检查 C:\WebRoot\index.html 是否存在
│ ├─> 存在 -> 返回静态文件
│ └─> 不存在 -> 进入路由匹配
│
└──> 匹配路由 /index.html混合模式(静态文件 + API)
vb
Private Sub Form_Load()
Set Server = New cHttpServer
' ========== 配置路由 ==========
' API 控制器
Call Server.Router.Reg("Api", New cApiController)
Call Server.Router.Add("/api/users", "Api@Users", OnlyGet)
Call Server.Router.Add("/api/data", "Api@Data", OnlyGet)
' 业务控制器
Call Server.Router.Reg("User", New cUserController)
Call Server.Router.Add("/user/login", "User@Login", OnlyPost)
' ========== 启动服务 ==========
' WebRoot 用于静态文件,API 请求会进入路由
Server.Start 8080, "C:\WebRoot"
Debug.Print "服务启动成功"
Debug.Print " 前端: http://localhost:8080/"
Debug.Print " API: http://localhost:8080/api/users"
End Sub单页应用 (SPA) 支持
对于 React/Vue/Angular 等 SPA,需要配置所有路由都返回 index.html:
vb
' cSpaController.cls
Public Sub Index(ctx As cHttpServerContext)
' 返回 index.html 让前端路由处理
ctx.Response.File "/index.html"
End Sub
' 注册路由
Private Sub Form_Load()
Set Server = New cHttpServer
' API 路由
Call Server.Router.Reg("Api", New cApiController)
Call Server.Router.Add("/api/*", "Api@Handle")
' SPA 路由:所有非 API 请求返回 index.html
Call Server.Router.Reg("Spa", New cSpaController)
Call Server.Router.Add("/*", "Spa@Index")
Server.Start 8080, "C:\WebRoot"
End Sub文件上传目录
vb
' 上传文件保存到静态目录
Public Sub Upload(ctx As cHttpServerContext)
' 保存上传的文件
Dim savePath As String
savePath = ctx.Server.WebRoot & "\upload\" & filename
Call SaveUploadFile(ctx.Request.RawBodyBin, savePath)
' 返回可访问的 URL
Dim result As New Scripting.Dictionary
result("url") = "/upload/" & filename
ctx.Response.Json result
End Sub缓存控制
vb
' cCacheMiddleware.cls
Public Sub Entry(ctx As cHttpServerContext)
' 静态文件添加缓存头
If IsStaticFile(ctx.Request.PathInfo) Then
' 缓存 1 小时
ctx.Response.Header("Cache-Control") = "public, max-age=3600"
ctx.Response.Header("Expires") = FormatHttpDate(DateAdd("h", 1, Now))
End If
End Sub
Private Function IsStaticFile(path As String) As Boolean
Dim ext As String
ext = LCase(Mid(path, InStrRev(path, ".")))
IsStaticFile = (ext = ".css" Or ext = ".js" Or ext = ".png" Or _
ext = ".jpg" Or ext = ".gif" Or ext = ".ico")
End Function最后更新: 2026-05-17