Notice there’s no Xcode project in your template even though you’ve built and run the app. This is deliberate. In fact, the project file is explicitly excluded from source control using the .gitignore file. When using SPM, Xcode projects are discardable and regenerated whenever you make project changes.
故意不创建 Xcode project 并设置了.gitngnore,为什么呢?
而且运行vapor xcode -y
又创建了一个 Xcode project.
猜测是因为跨平台兼容目的,毕竟Linux平台没有Xcode。
了解到的一个原因是有一些非代码层面的配置问题往往是由于 Xcode project 的配置不对引起的,所以有的时候它们会推荐直接重新初始化 Xcode project,另外 Vapor 项目 Xcode project 也并不是必须项。
URL入参
EG1
router.get("hello", "vapor") { req -> String in
return "Hello Vapor!"
}
Each parameter to router.get is a path component in the URL. This route is invoked when a user enters http://localhost:8080/hello/vapor as the URL.
get代表的是接收GET形式的访问。
参数是路由接收的URL参数,如上代码接收http://localhost:8080/hello/vapor形式的访问.
并返回"Hello Vapor!"字符串
EG2
// 1
router.get("hello", String.parameter) { req -> String in
//2
let name = try req.parameters.next(String.self)
// 3
return "Hello, \(name)!"
}
1 表明接收一个String形式的入参,内容任意
2 从Request对象中提取入参中的参数
如上代码接收http://localhost:8080/hello/Tim 并返回"Hello Tim!"字符串
入参冲突
由于EG2和EG3都接收http://localhost:8080/hello/vapor作为入参,因此测试了一下情况。
原以为是按代码顺序排优先级,但按我的理解实际上是按精确匹配作为高优先级,不管代码顺序如何,永远是精确匹配的router.get("hello", "vapor")
做出相应。匹配不上时才会匹配router.get("hello", String.parameter)
POST 入参
struct InfoData: Content {
let name: String
}
Vapor uses
Content
to extract the request data, whether it’s the default JSON-encoded or form URL-encoded.
继承自Content
,Content
是一个Vapor对Codable
的封装
一个接收InfoData入参的POST请求描述如下:
router.post(InfoData.self, at: "info") { req, data -> String in
return "Hello \(data.name)!"
}
网友评论