logan comments

This commit is contained in:
Tanner Nelson 2016-08-03 15:58:18 -04:00
parent 4a1df2b538
commit 15b924f22d
No known key found for this signature in database
GPG Key ID: 9C24375C64856B76
4 changed files with 40 additions and 11 deletions

View File

@ -4,7 +4,7 @@ template:
title: Vapor Documentation
subTitle: A web framework and server for Swift that works on macOS and Ubuntu.
baseUrl: https://qutheory.github.io/documentation
baseUrl: https://vapor.github.io/documentation
menu:
sections:

View File

@ -26,7 +26,7 @@ This will return a greeting for any HTTP method or content type that the `name`
To specifically target JSON, use the `request.json` property.
```
```swift
drop.post("json") { request in
guard let name = request.json["name"].string else {
throw Abort.badRequest
@ -44,8 +44,8 @@ To respond with JSON, simply wrap your data structure with `JSON(node: )`
```swift
drop.get("version") { request in
return try JSON(node: [
"version": "1.0
"])
"version": "1.0"
])
}
```

View File

@ -46,7 +46,7 @@ An alternate syntax that accepts a `Method` as the first parameter is also avail
```swift
drop.add(.trace, "welcome") { request in
return "Hello"
return "Hello"
}
```
@ -70,7 +70,7 @@ A custom [Response](../http/response.md) can be returned.
```swift
drop.get("vapor") { request in
return Response(redirect: "http://vapor.codes")
return Response(redirect: "http://vapor.codes")
}
```
@ -88,7 +88,7 @@ A lot of types in Vapor conform to this protocol by default:
```swift
drop.get("json") { request in
return try JSON(node: [
return try JSON(node: [
"number": 123,
"text": "unicorns",
"bool": false
@ -104,7 +104,7 @@ If you are unable to return a response, you may `throw` any object that conforms
```swift
drop.get("404") { request in
throw Abort.notFound
throw Abort.notFound
}
```
@ -112,7 +112,7 @@ You can customize the message of these errors by using `Abort`
```swift
drop.get("error") { request in
throw Abort.custom(status: .badRequest, message: "Sorry 😱")
throw Abort.custom(status: .badRequest, message: "Sorry 😱")
}
```
@ -120,9 +120,27 @@ These errors are caught by default in the `AbortMiddleware` where they are turne
```json
{
error: true,
message: "<the message>"
error: true,
message: "<the message>"
}
```
If you want to override this behavior, remove the `AbortMiddleware` from the `Droplet`'s middleware and add your own.
## Fallback
Fallback routes allow you to match multiple layers of nesting slashes.
```swift
app.get("anything", "*") { request in
return "Matches anything after /anything"
}
```
For example, the above route matches all of the following and more:
- /anything
- /anything/foo
- /anything/foo/bar
- /anything/foo/bar/baz
- ...

View File

@ -119,3 +119,14 @@ drop.get("v1", "users", ":userId", "posts", ":postId", "comments": ":commentId")
```
Request parameters can be accessed either as a dictionary or using the `extract` syntax which throws instead of returning an optional.
### Groups
Manual request parameters also work with [groups](group.md).
```swift
let userGroup = drop.grouped("users", ":userId")
userGroup.get("messages") { req in
let user = try req.parameters.extract("userId") as User
}
```