Size: 1899
Comment:
|
← Revision 9 as of 2023-10-11 13:05:59 ⇥
Size: 2470
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
Framework with integrated OpenAPI compiler to build Node.js serve-side applications using TypeScript. | Framework with integrated OpenAPI compiler to build Node.js serve-side applications using [[typescript]]. |
Line 29: | Line 29: |
curl -vvv localhost:3000/hello/world | |
Line 32: | Line 33: |
== tsoa.json == | == tsoa.json == |
Line 73: | Line 74: |
}}} === src/hello/helloResponse.ts === {{{#!highlight typescript export interface HelloResponse { response: string; } }}} === src/hello/helloController.ts === {{{#!highlight typescript import {Controller,Get,Path,Route,} from "tsoa"; import { HelloResponse } from "./helloResponse"; @Route("hello") export class HelloController extends Controller { @Get("{name}") public async getHello(@Path() name: string ): Promise<HelloResponse> { return { response: "Hello " + name }; } } }}} |
tsoa
Framework with integrated OpenAPI compiler to build Node.js serve-side applications using typescript.
Example
1 npm init
2 npm install ts-node typescript express @types/express tslint cors @types/cors tsoa @types/tsoa swagger-ui-express @types/swagger-ui-express
3 mkdir -p src/hello
4 touch src/index.ts
5 touch src/hello/helloController.ts
6 touch src/hello/helloResponse.ts
7
8 mkdir -p build
9 # create tsconfig
10 npx tsc --init
11 touch README.md
12 # ts-node src/index.ts
13 # enable "experimentalDecorators": true, in tsconfig.json
14 # enable "resolveJsonModule": true, in tsconfig.json
15 # in script start in package.json put
16 # npx tsoa swagger && npx tsoa spec-and-routes && ts-node src/index.ts
17 npm start
18 # http://localhost:3000/
19 curl -vvv localhost:3000
20 curl -vvv localhost:3000/hello/world
21 curl -vvv localhost:3000/swagger/
tsoa.json
src/index.ts
1 import express from 'express';
2 import cors from 'cors';
3 import bodyParser from 'body-parser';
4 import { RegisterRoutes } from "../build/routes";
5 import * as swaggerJson from '../build/swagger.json';
6 import * as swaggerUI from 'swagger-ui-express';
7
8 const port = process.env.PORT || 3000;
9 const app = express();
10 app.use(cors());
11 app.use(bodyParser.json());
12 app.use(["/openapi", "/docs", "/swagger"], swaggerUI.serve, swaggerUI.setup(swaggerJson));
13 RegisterRoutes(app);
14
15 app.get('/', (req, res) => {
16 res.send('Hello');
17 });
18
19 app.listen(port, () => {
20 console.log('Listening in port ' + port);
21 });
src/hello/helloResponse.ts
src/hello/helloController.ts
1 import {Controller,Get,Path,Route,} from "tsoa";
2 import { HelloResponse } from "./helloResponse";
3
4 @Route("hello")
5 export class HelloController extends Controller {
6 @Get("{name}")
7 public async getHello(@Path() name: string ): Promise<HelloResponse> {
8 return { response: "Hello " + name };
9 }
10 }