npm init # and follow the resulting prompts to set up the project npm i koa koa-router npm i --save-dev typescript ts-node nodemon npm i --save-dev @types/koa @types/koa-router
[nodemon] 1.18.4 [nodemon] to restart at any time, enter `rs` [nodemon] watching: src/***/* [nodemon] starting `ts-node ./src/server.ts` Server running on port 8080
[nodemon] 1.18.4 [nodemon] to restart at any time, enter `rs` [nodemon] watching: src/***/* [nodemon] starting `ts-node ./src/server.ts` Server running on port 8080 Url: / Url: / Url: /blog Url: /blog
第五步、标准中间件
显然,你真的不想为你的网络应用重新发明轮子。 根据您要创建的应用程序类型,以下中间件可能很有用:
Koa路由器 https://github.com/alexmingoia/koa-router
Koa Body Parser(用于JSON和Form Data支持) https://github.com/dlau/koa-body
import * as bodyParser from"body-parser"; import * as cookieParser from"cookie-parser"; import * as express from"express"; import * as logger from"morgan"; import * as path from"path"; import errorHandler = require("errorhandler"); import merhodOverride = require("method-override");
/** * The Server * * @classServer */ exportclassServer { public app: express.Application;
/** * Bootstrap the application * * @classServer * @methodbootstrap * @static * @return Returns the newly created injector for this app. Returns the newly created injector for this app. */ public staticbootstrap(): Server { returnnewServer(); }
/** * The Server * * @classServer */ exportclassServer { public app: express.Application;
/** * Bootstrap the application * * @classServer * @methodbootstrap * @static * @return Returns the newly created injector for this app. Returns the newly created injector for this app. */ public staticbootstrap(): Server { returnnewServer(); } }
Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).
/** * Add a JS external file to the request * * @classBaseRoute * @methodaddScript * @param src {string} The src to the external JS file * @return {BaseRoute} The self for chaining */ public addScript(src: string): BaseRoute { this.scripts.push(src); returnthis; }
/** * The home page route * * @classIndexRoute * @methodindex * @param req {Request} The express Request Object. * @param res {Response} The express Response Object. * @param next {NextFunction} Execute the next method. */ public index(req: Request, res: Response, next: NextFunction) { // set custom title this.title = "Home | TS Blog";
letoptions: Object = { "message": "Welcome to the TS Blog", };
$ tsc ./src/advanced_types_2.ts src/advanced_types_2.ts:45:10 - error TS2551: Property 'func1' does not exist on type'Type1Class | Type2Class'. Did you mean 'func2'? Property 'func1' does not exist on type'Type2Class'.
45 if (type.func1) { ~~~~~
src/advanced_types_2.ts:46:10 - error TS2551: Property 'func1' does not exist on type'Type1Class | Type2Class'. Did you mean 'func2'? Property 'func1' does not exist on type'Type2Class'.
46 type.func1(); // 报错 ~~~~~
src/advanced_types_2.ts:47:17 - error TS2551: Property 'func3' does not exist on type'Type1Class | Type2Class'. Did you mean 'func2'? Property 'func3' does not exist on type'Type1Class'.
47 } elseif (type.func3) { ~~~~~
src/advanced_types_2.ts:48:10 - error TS2551: Property 'func3' does not exist on type'Type1Class | Type2Class'. Did you mean 'func2'? Property 'func3' does not exist on type'Type1Class'.
$ tsc ./src/advanced_types_1.ts && node ./src/advanced_types_1.js src/advanced_types_1.ts:65:37 - error TS2345: Argument of type'true' is not assignable to parameter of type'string | number'.
$ tsc ./src/advanced_types_1.ts src/advanced_types_1.ts:111:6 - error TS2551: Property 'func1' does not exist on type'Type1Class | Type2Class'. Did you mean 'func2'? Property 'func1' does not exist on type'Type2Class'.
111 type.func1();
这里的联合类型可能有点复杂,但是你很容易就习惯了。 如果一个值的类型是 A | B,我们能够 确定的是它包含了 A 和 B中共有的成员。 这个例子里, Type1Class具有一个func1成员。 我们不能确定一个 Type1Class | Type2Class类型的变量是否有func1方法。 如果变量在运行时是Type1Class类型,那么调用type.func1()就出错了。
$ npx ts-node src/type_compatibility_3.ts ⨯ Unable to compile TypeScript: src/type_compatibility_3.ts(8,1): error TS2322: Type 'Generics<string>' is not assignable to type'Generics<number>'. Type 'string' is not assignable to type'number'.