可以通过友好的响应对象操作 Http 响应
大部分情况只要直接在控制器方法返回数据即可
import { Controller, Get } from '@tiger/common'
@Controller('/users')
export class UserController {
@Get('/:id')
show(id: number) {
return `user id: ${id}`
}
}
也可以使用 Response 实例
import { Controller, Get, Response } from '@tiger/common'
@Controller('users')
export class UserController {
@Get()
show(id: number) {
return new Response('im a data', 200)
}
}
new Response([data [, code = 200 [, headers = {}]]])Response 构造函数 可以接收3个参数
data: 响应数据code: 响应状态码, 默认200headers: 响应头我们也可以使用 response.setData([data]) 方法设置需要响应的数据
import { Controller, Get, Response } from '@tiger/common'
@Controller('posts')
export class PostController {
@Get()
index() {
return new Response().setData({
name: 'Tiger'
})
}
}
使用 response.setCode([code]) 方法设置需要响应状态码
import { Controller, Get, Response } from '@tiger/common'
@Controller('posts')
export class PostController {
@Get()
index() {
return new Response().setData({
name: 'Tiger'
}).setCode(200)
}
}
使用 response.setHeader(<name>, [value]) 方法设置响应头
import { Controller, Get, Response } from '@tiger/common'
@Controller('posts')
export class PostController {
@Get()
index() {
return new Response()
.setData({ name: 'Zewail' })
.setCode(200)
.setHeader('content-type', 'application/json')
.setHeader({ 'Version': 'v1' })
}
}
setData、setCode、setHeader 等方法支持链式调用,状态码默认为 200
使用 response.success([data [, code = 200]]) 方法返回一个成功响应:
import { Controller, Get, Response } from '@tiger/common'
@route('/posts')
export class PostController {
@Get('/:id')
show(id) {
return new Response().success('im a data', 200)
}
}
框架对常用成功状态码进行了封装,可以简单的调用这些方法,返回想要的信息和状态码:
import { Controller, Get, Response } from '@tiger/common'
@Controller('/users')
export class UserController {
@Get('/:id')
show(id) {
// 返回一个 200 状态码的响应
return new Response().OK('im a data')
}
}
所有方法都提供了对应的
小驼峰和大驼峰写法
| 状态码 | 方法 |
|---|---|
| 100 | Continue() |
| 101 | SwitchingProtocols() |
| 102 | Processing() |
| 103 | EarlyHints() |
| 200 | OK() |
| 201 | Created() |
| 202 | Accepted() |
| 203 | NonAuthoritativeInformation() |
| 204 | NoContent() |
| 205 | ResetContent() |
| 206 | PartialContent() |
| 207 | MultiStatus() |
| 208 | AlreadyReported() |
| 226 | IMUsed() |
| 300 | MultipleChoices() |
| 302 | Found() |
| 303 | SeeOther() |
| 304 | NotModified() |
| 305 | UseProxy() |
| 306 | Unused() |
| 307 | TemporaryRedirect() |
| 308 | PermanentRedirect() |
使用 response.error([message [, code = 404]]) 方法来抛出一个 http 异常:
import { Controller, Get, Response } from '@tiger/common'
@Controller('users')
export class UserController {
@Get(':id')
show(id) {
// ...
if (!user) {
// 抛出一个404异常
return new Response().error('no user!', 404)
}
}
}
框架对常用错误状态码进行了封装,可以简单的调用这些方法,返回想要的信息和状态码:
import { Controller, Get, Response } from '@tiger/common'
@Controller('users')
export class UserController {
@Get(':id')
show(id) {
// ...
if (!user) {
// 抛出一个404异常
return new Response().NotFound('no user!')
}
}
}
| 状态码 | 方法 |
|---|---|
| 400 | BadRequest() |
| 401 | Unauthorized() |
| 402 | PaymentRequired() |
| 403 | Forbidden() |
| 404 | NotFound() |
| 405 | MethodNotAllowed() |
| 406 | NotAcceptable() |
| 407 | ProxyAuthenticationRequired() |
| 408 | RequestTimeout() |
| 409 | Conflict() |
| 410 | Gone() |
| 411 | LengthRequired() |
| 412 | PreconditionFailed() |
| 413 | PayloadTooLarge() |
| 414 | URITooLong() |
| 415 | UnsupportedMediaType() |
| 416 | RangeNotSatisfiable() |
| 417 | ExpectationFailed() |
| 418 | ImATeapot() |
| 421 | MisdirectedRequest() |
| 422 | UnprocessableEntity() |
| 423 | Locked() |
| 424 | FailedDependency() |
| 425 | UnorderedCollection() |
| 426 | UpgradeRequired() |
| 428 | PreconditionRequired() |
| 429 | TooManyRequests() |
| 431 | RequestHeaderFieldsTooLarge() |
| 451 | UnavailableForLegalReasons() |
| 500 | InternalServerError() |
| 501 | NotImplemented() |
| 502 | BadGateway() |
| 503 | ServiceUnavailable() |
| 504 | GatewayTimeout() |
| 505 | HTTPVersionNotSupported() |
| 506 | VariantAlsoNegotiates() |
| 507 | InsufficientStorage() |
| 508 | LoopDetected() |
| 509 | BandwidthLimitExceeded() |
| 510 | NotExtended() |
| 511 | NetworkAuthenticationRequired() |
使用 Redirect 实例:
import { Controller, Get, Redirect } from '@tiger/common'
@Controller()
export class User extends BaseController {
@Get(':id')
show(id) {
return new Redirect('http://www.google.com/')
}
}
使用 Redirect 实例的 go 方法:
import { Controller, Get, Redirect } from '@tiger/common'
@controller()
export default class Post extends BaseController {
@Get('/redirect')
redirect() {
return new Redirect().go('/index')
}
}
使用 download来下载文件
import { Controller, Get, Response } from '@tiger/common'
@Controller()
export class PostController {
@Get()
download() {
// store success
const fileStream = /* 创建文件流 */
return new Response().download(fileStream, 'xxx.pdf')
}
}