本文将使用 Mocket
来搭建 MoonBit Web 服务器
moon add oboard/mocket
安装 mocket
包start.sh
启动:sudo chmod +x ./start.sh
./start.sh
start.bat
启动:start.bat
// Example usage of mocket package in MoonBit
fn main {
let server = @mocket.listen(get_context(), 4000)
server.get("/", fn(_req, _res) {
@mocket.html("<h1>Hello, World!</h1>")
})
server.get("/text", fn(_req, _res) {
String("<h1>Hello, World!</h1>")
})
server.get(
"/data",
fn(_req, _res) {
{
"name": "John Doe",
"age": 30,
"city": "New York"
}
},
)
server.post(
"/echo",
fn(req, _res) {
match req {
{ "body": data } => data
_ => String("No data received")
}
},
)
}