Swift - 使用HTML的蒸气
问题描述:
我最近从Perfect转换到Vapor。在Perfect中,你可以做这样的事情而不使用html文件。Swift - 使用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()
}
)
蒸气,我发现返回的HTML的唯一办法就是做我this.How可以在不使用蒸汽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
}
+0
谢谢我没有完全搜索文档。 –
你有什么事也问题吗?请修改您的帖子并提供一个明确的问题。 –