xChar
·4 months ago

本文将使用 Mocket 来搭建 MoonBit Web 服务器

image

image

Usage

首先需要准备

Linux/MacOS 下使用 start.sh 启动:

sudo chmod +x ./start.sh
./start.sh

Windows 下使用 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")
      }
    },
  )
}


Loading comments...