Swift - Vapor 使用 HTML

8

我最近从 Perfect 切换到 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中,我发现惟一返回HTML的方法是这样做。如何在不使用HTML文件的情况下返回HTML代码?
 drop.get("/") { request in
    return try drop.view.make("somehtmlfile.html")
} 

你也有关于某些事情的问题吗?请编辑您的帖子并包含一个明确的问题。 - Sami Kuhmonen
1个回答

12
你可以构建自己的 响应(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
}

谢谢,我没有完全查阅文档。 - James Ikeler
第二个不再起作用,因为下标是不可修改的。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接