代码拉取完成,页面将自动刷新
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---|---|---|---|---|---|
All files | 86.98 | 76.92 | 84.62 | 86.91 | |
ts-restful | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
ts-restful/core | 87.93 | 68.12 | 82.05 | 87.57 | |
AbstractController.ts | 100 | 100 | 100 | 100 | |
constant.ts | 100 | 100 | 100 | 100 | |
decorator.ts | 92.44 | 80.43 | 90.63 | 92.17 | ... ,38,70,143,175 |
swagger.ts | 66.67 | 43.48 | 33.33 | 66.67 | ... 05,207,215,216 |
ts-restful/core/router | 86.03 | 81.29 | 88.46 | 86.12 | |
index.ts | 86.03 | 81.29 | 88.46 | 86.12 | ... 68,371,373,374 |
当我们使用 nodejs express开发web项目时,是否写了很多 路由和 controller 映射的代码?我们的路由规则是否是按照restful规范去实现的呢?
ts-express-restful帮你去简化路由与controller映射的样板代码,如果按照restfu规范,你只需要关注资源提供方式,剩下的交给ts-express-restful.
当然如果你想自定义路由也很简单,只需要简单一句注解就好,感谢spring mvc 给了我很大的参考。
此处ctrl为controller类名 去掉 Controller后缀.如类名为 UserController, ctrl为 user;
Controller函数名 | 映射的Http Method | 映射的路由地址 |
---|---|---|
find | /ctrl/ | GET |
get | /ctrl/:id | GET |
update | /ctrl/:id | PUT |
add | /ctrl/ | POST |
delete | /ctrl/:id | DELETE |
@Router("/other", "GET") | /ctrl/other | GET |
@Restful @Restful(mountUrl)
@Router(url, method?: string, options: any)
@RequestMapping @PostMapping @GetMapping
scannerDecorator
registerControllerToRouter
(controller instance).$isValidId
(controller instance).$before
@ResponseBody() 直接在函数中return默认将结果输出
@SchemaFilter(schema: any, checkType: boolean)
@Autowire
@Service
@RequestBody @RequestBodyParam @RequestParam @HttpRequest @HttpResponse @QueryStringParam @Header @Cookie
// router/index.ts
import http = require("http");
import {scannerControllers, registerControllerToRouter} from "ts-express-restful";
import path = require("path");
import express = require("express");
var router = express.Router();
scannerControllers(path.join(__dirname, 'controller'));
//挂载到express
registerControllerToRouter(router);
export async function initHttp(app) {
app.use('/api/v1', router);
}
//或者挂载到koa
// import * as Router from 'koa-router';
// var app = new Koa();
// var route = new Router();
//registerControllerToKoaRouter(router);
// app.use(route.routes());
// controller/city.ts
import {AbstractController, Restful, Router} from "ts-express-restful";
//此处可以是 @Restful('/test')
@Restful()
export class TestController extends AbstractController {
constructor() {
super();
}
$isValidId(id: string) {
//只有ID是数字是才认为是ID 如 /test/1 可以请求到get /test/xx 为返回404 /test/other 为映射到 other函数
return /^\d+$/.test(id);
}
// 调用地址为 /test/:id
async get(@RequestParam id: number) {
return {
id
}
}
// 调用地址为 /test/other
@Router("/other")
async other(@RequestParam other: string) {
return {
other,
}
}
}
@Service()
export class TestService {
sayHello() {
return 'hello world';
}
}
@Restful
export class TestController {
@Autowire
test: TestService;
@GetMapping("/helloword")
sayHello() {
return this.test.sayHello();
}
}
@Restful()
export class TestController {
@PostMapping("/test")
async test(@RequestBody test: Test) {
return test;
}
@GetMapping("/test2")
async test2(@QueryStringParam keyword: string, @QueryString password: string) {
return {keyword, password};
}
@PostMapping("/test3")
async test3(@HttpRequest req: Request) {
let body = req.body;
return body;
}
}
swagger 文档默认挂在在 /swagger 路径下
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。