一直很喜欢 flask 的路由写法,看到 TS 也有 decorator 就搞了一个。
import {Router, HttpRequest, BaseController} from "yattsf"
export class HelloController extends BaseController {
@Router.get('/')
hello(req:HttpRequest) {
return 'hello';
}
}
1
EPr2hh6LADQWqRVH 2016-12-14 11:56:20 +08:00
我也写了一个,不过是基于 koa
``` import { RouteHost, Middleware, Controller, ExtendedContext, NextCallback, RouteHub } from 'petropub-dev-lib'; export class HelloWorldBaseRoutes extends RouteHost { @Middleware('/666', { restrict: undefined }) helloWorld__(ctx: ExtendedContext, next: NextCallback) { ctx.body += '333'; return next(); } }; export class HelloWorldRoutes extends HelloWorldBaseRoutes { @Controller('GET', '/666') helloWorld(ctx: ExtendedContext, next: NextCallback) { ctx.body += '6666'; return next(); } @Middleware('/666', { restrict: undefined }) helloWorld_(ctx: ExtendedContext, next: NextCallback) { ctx.body += '5555'; return next(); } }; let theHelloWorldHub = new RouteHub(); new HelloWorldRoutes(theHelloWorldHub); export { theHelloWorldHub } ``` |
3
jswh OP |