html - Swift - 使用 HTML 的 Vapor

标签 html swift perfect vapor

我最近从 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/

相关文章:

swift - 在 Swift Linux 中使用 'Any' 和 'AnyObject' 类型会出错

swift - Perfect 2.0 的 Heroku Procfile

javascript - Chrome App webview 和触摸滚动传播

javascript - 需要帮助修改网站的 javascript

javascript - JavaFX 2.2 WebEngine HTML 选择下拉样式

javascript - 通过 javascript 中的类属性应用样式时是否有限制?

ios - 如何将数据从父 View Controller 传递到子容器 View Controller

ios - 为什么我不能用计时器停止进度条?

ios - 删除从其子项引用的 UIView?

linux - 完美/快速编译在 OSX 中工作,在 Linux 中失败