Swift 蒸汽3路由 路由问题
我正在使用Vapor的最新版本,并试图学习它 我正在尝试在控制器中创建路由。我已在routes.swift文件中注册了控制器。我现在需要在控制器文件中正确注册路由 我已经使用RouteCollection扩展了这个类,并且正在为post请求编写一个路由。我打算传递一个JSON对象,并拥有一个从内容扩展而来的类,以便更容易从JSON数据创建对象 post请求然后将数据提交到FoundationDB数据库。我在硬编码时读写它,但现在需要使用请求发送数据 这就是我所拥有的Swift 蒸汽3路由 路由问题,swift,vapor,Swift,Vapor,我正在使用Vapor的最新版本,并试图学习它 我正在尝试在控制器中创建路由。我已在routes.swift文件中注册了控制器。我现在需要在控制器文件中正确注册路由 我已经使用RouteCollection扩展了这个类,并且正在为post请求编写一个路由。我打算传递一个JSON对象,并拥有一个从内容扩展而来的类,以便更容易从JSON数据创建对象 post请求然后将数据提交到FoundationDB数据库。我在硬编码时读写它,但现在需要使用请求发送数据 这就是我所拥有的 func boot(rout
func boot(router: Router) throws {
router.post( ) { // need to send JSON data in the request to the createCountry function
}
func createCountry( ) { // I need to put the JSON data into a class called Country which has three string fields; country_name, Timezone and default_location. This will then be written to the foundationDB
}
如何格式化router.post()以及如何格式化createCountry()函数存根?我一直在输入req:Request和各种->都没有用。很明显,我在基本层面上做错了什么。我不知道我是否理解,但要注册路由,然后处理请求,您应该这样写:
class CountryController: RouteCollection {
// Register routes for country
func boot(router: Router) throws {
let group = router.grouped("api", "country")
group.post(Country.self, at: "new", use: newCountryHandler)
}
}
private extension CountryController {
func newCountryHandler(_ request: Request, newCountry: Country) throws -> Future<HTTPResponseStatus> {
// Handle your new Country object
}
}
class CountryController:RouteCollection{
//登记国家/地区的路线
func引导(路由器:路由器)抛出{
let group=router.grouped(“api”、“国家”)
group.post(Country.self,地址:“new”,用法:newCountryHandler)
}
}
专用扩展控制器{
func newCountryHandler(request:request,newCountry:Country)抛出->未来{
//处理您的新国家/地区对象
}
}
这看起来比我一直尝试的要好。创造什么样的未来一直困扰着我,但httprespone的状态看起来不错。谢谢