我最近从 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 的唯一方法就是这样做。如何在不使用 vapor 中的 html 文件的情况下返回 html 代码?
drop.get("/") { request in
return try drop.view.make("somehtmlfile.html")
}
最佳答案
您可以构建自己的 Response , 完全避免 View 。
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
}
关于html - Swift - 使用 HTML 的 Vapor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40445506/