Thanks to visit codestin.com
Credit goes to github.com

Skip to content

T Koa 中的 TypeScript

Bd999 edited this page Apr 24, 2019 · 7 revisions

TypeScript

为了使用 typescript 著名的类型检查,您需要定义一些接口:

import tKoa = require('tkoa');

// 定义接口
interface ctx {
    response: {
        end: Function
    }
}

const app = new tKoa();

// 响应
app.use((ctx: ctx) => {
    ctx.response.end('Hello T-koa!');
});

app.listen(3000);

或者你也可以这样写:

import tKoa = require('tkoa');

const app = new tKoa();

// 响应的同时定义接口
app.use((ctx: {response: {end: Function}}) => {
    ctx.response.end('Hello T-koa!');
});

app.listen(3000);

如果你不需要类型检查系统,您也可以直接使用 any 类型 (虽然我们并不推荐)

import tKoa = require('tkoa');

const app = new tKoa();

// 这里使用了 any 类型
app.use((ctx: any) => {
    ctx.response.end('Hello T-koa!');
});

app.listen(3000);

如果您了解需要更详细的信息,请访问 typescript官网

Context

由于t-koa是基于 TypeScript 的,所以您需要了解某些值的类型才能更好地工作:

API

  • ctx.req [object]
  • ctx.res [object]
res.statusCode [number]
res.writeHead()
res.write()
res.end()
  • ctx.request [object]
  • ctx.response [object]
  • ctx.state [object]
  • ctx.app [app(object)]
  • ctx.app.emit [function]
  • ctx.cookies.get(name,[options])
  • ctx.cookies.set(name, value, [options])
  • ctx.throw([status], [msg], [properties])
  • ctx.assert(value, [status], [msg], [properties])
  • ctx.respond [boolean]

Request aliases

以下访问器和 Request 别名等效:

  • ctx.header [object]
  • ctx.headers [object]
  • ctx.method [string]
  • ctx.method=
  • ctx.url [string]
  • ctx.url=
  • ctx.originalUrl [string]
  • ctx.origin [string]
  • ctx.href [string]
  • ctx.path [string]
  • ctx.path=
  • ctx.query [object]
  • ctx.query=
  • ctx.querystring [string]
  • ctx.querystring=
  • ctx.host [string]
  • ctx.hostname [string]
  • ctx.fresh [boolean]
  • ctx.stale [boolean]
  • ctx.socket [object]
  • ctx.protocol [string]
  • ctx.secure [boolean]
  • ctx.ip [string]
  • ctx.ips [object]
  • ctx.subdomains [object]
  • ctx.is(types...)
  • ctx.accepts(types)
  • ctx.acceptsEncodings(types)
  • ctx.acceptsCharsets(charsets)
  • ctx.acceptsLanguages(types)
  • ctx.get(field)

Response aliases

以下访问器和 Response 别名等效:

  • ctx.body
  • ctx.body=
string written
Buffer written
Stream piped
Object || Array json-stringified
null no content response
  • ctx.status [number]
  • ctx.status=
100 "continue"
101 "switching protocols"
102 "processing"
200 "ok"
201 "created"
202 "accepted"
203 "non-authoritative information"
204 "no content"
205 "reset content"
206 "partial content"
207 "multi-status"
208 "already reported"
226 "im used"
300 "multiple choices"
301 "moved permanently"
302 "found"
303 "see other"
304 "not modified"
305 "use proxy"
307 "temporary redirect"
308 "permanent redirect"
400 "bad request"
401 "unauthorized"
402 "payment required"
403 "forbidden"
404 "not found"
405 "method not allowed"
406 "not acceptable"
407 "proxy authentication required"
408 "request timeout"
409 "conflict"
410 "gone"
411 "length required"
412 "precondition failed"
413 "payload too large"
414 "uri too long"
415 "unsupported media type"
416 "range not satisfiable"
417 "expectation failed"
418 "I'm a teapot"
422 "unprocessable entity"
423 "locked"
424 "failed dependency"
426 "upgrade required"
428 "precondition required"
429 "too many requests"
431 "request header fields too large"
500 "internal server error"
501 "not implemented"
502 "bad gateway"
503 "service unavailable"
504 "gateway timeout"
505 "http version not supported"
506 "variant also negotiates"
507 "insufficient storage"
508 "loop detected"
510 "not extended"
511 "network authentication required"
  • ctx.message [string]
  • ctx.message=
  • ctx.length [number]
  • ctx.length=
  • ctx.type [string]
  • ctx.type=
ctx.type = 'text/plain; charset=utf-8';
ctx.type = 'image/png';
ctx.type = '.png';
ctx.type = 'png';
  • ctx.headerSent
  • ctx.redirect(url, [alt])
  • ctx.attachment([filename], [options])
  • ctx.set(field, value)
  • ctx.append(field, value)
  • ctx.remove(field)
  • ctx.lastModified=
  • ctx.etag=

Clone this wiki locally