Docs / Tutorial
12 · HTTP FileServer
Serve a directory in one call:
http.listen_and_serve("127.0.0.1:8080", ".")Default root is the example’s www/; pass a directory argument to override.
Source
@main
#net/http
// http.listen_and_serve("127.0.0.1:8080", ".")
//
// 参数:要对外的目录。默认本示例的 www/(从仓库根运行)。
fn main() int {
mut root := "examples/12_file_server/www"
av := args()
if av.len >= 2 {
root = av[1] + ""
}
println("FileServer " + root)
println("http://127.0.0.1:8080")
http.listen_and_serve("127.0.0.1:8080", root) or (err) {
println(err.msg())
<< 1
}
<< 0
}
Run from the separate examples repository root; source files live under examples/.