創建使用工具箱命令一個新項目:汽新項目名稱如何編寫一個蒸汽網頁框架的中間件?
在main.swift文件我加了中間件代碼:
import Vapor
import HTTP
final class VersionMiddleware: Middleware {
func respond(to request: Request, chainingTo next: Responder) throws -> Response {
let response = try next.respond(to: request)
response.headers["Version"] = "API v1.0"
print("not printing")
return response
}
}
let drop = Droplet(availableMiddleware: [
"version": VersionMiddleware()
])
drop.get("hello") {
req in
return "Hello world"
}
drop.run()
但是當我運行它,它打印的「Hello World」,但API版本不添加到標題。我正在用郵差檢查它。