微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift – 使用HTML的蒸汽

我最近从 Perfect切换到 Vapor.In 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")
}
您可以构建自己的 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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐