使用工具箱命令创建了一个新项目:vapor new projectname
在 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 版本不会添加到标题中。我正在使用邮递员检查它。