Swift - Vapor 使用 HTML

Swift - Vapor Using HTML

我最近从 Perfect to Vapor 进行了切换。在 Perfect 中,您可以在不使用 html 文件的情况下执行类似的操作。

routes.add(method: .get, uri: "/", handler: {
    request, response in
    response.setHeader(.contentType, value: "text/html")
    response.appendBody(string: "<html><img src=http://www.w3schools.com/html/pic_mountain.jpg></html>")
    response.completed()
   }
)

在 Vapor 中,我发现 return html 的唯一方法是 this.How 我可以 return html 代码而不使用 html 文件在蒸汽中?

 drop.get("/") { request in
    return try drop.view.make("somehtmlfile.html")
} 

您可以构建自己的 Response,完全避免视图。

drop.get { req in
  return Response(status: .ok, headers: ["Content-Type": "text/html"], body: "<html><img src=http://www.w3schools.com/html/pic_mountain.jpg></html>")
}

drop.get { req in
  let response = Response(status: .ok, body: "<html><img src=http://www.w3schools.com/html/pic_mountain.jpg></html>")
  response.headers["Content-Type"] = "text/html"
  return response
}