Created
February 5, 2019 17:09
-
-
Save 29satnam/d18ab59fc7f894f5c39aa10221c55795 to your computer and use it in GitHub Desktop.
Vapor very basic route handeling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vapor | |
/// Register your application's routes here. | |
public func routes(_ router: Router) throws { | |
/* // Example of configuring a controller | |
let todoController = TodoController() | |
router.get("todos", use: todoController.index) | |
router.post("todos", use: todoController.create) | |
router.delete("todos", Todo.parameter, use: todoController.delete) */ | |
//Simple GET | |
router.get { req in | |
return "It works!" | |
} | |
// GET | |
router.get(String.parameter) { req in | |
return "Hello \(try req.parameters.next(String.self))" | |
} | |
// POST | |
router.post(InfoData.self, at: "info") { req, data -> InfoResponse in | |
return InfoResponse(request: data) | |
} | |
} | |
// Post encoder | |
struct InfoData: Content { | |
let name: String | |
} | |
// Response encoder | |
struct InfoResponse: Content { | |
let request: InfoData | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment