@andrewcaires/express
v1.2.2
Published
Decorators for express
Downloads
42
Readme
express
Decorators for express
Installation
The module is now available on npm! npm i @andrewcaires/express
Example usage
// index.ts
import { Application, Argument, Body, Context, Controller, Ctx, Delete, Extends, Get, Middleware, Params, Post, Put } from ".";
@Controller("/test")
class TestController {
public middleware(
@Ctx() ctx: Context,
@Argument() arg: string
): void {
console.log("middleware", arg);
ctx.next();
}
@Post()
@Middleware("middleware", "add")
public add(
@Ctx() ctx: Context,
@Body() body: any
): void {
ctx.json(body);
}
@Get()
@Middleware("middleware", "all")
public all(
@Ctx() ctx: Context
): void {
ctx.json([1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
@Delete("/:id")
@Middleware("middleware", "del")
public del(
@Ctx() ctx: Context,
@Params("id") id: string
): void {
ctx.success(id);
}
@Get("/:id")
@Middleware("middleware", "get")
public get(
@Ctx() ctx: Context,
@Params("id") id: string
): void {
ctx.json({ id, name: "TestController" });
}
@Put("/:id")
@Middleware("middleware", "set")
public set(
@Ctx() ctx: Context,
@Params("id") id: string
): void {
ctx.success(id);
}
}
@Controller("/test2")
@Extends(TestController)
class Test2Controller extends TestController {
@Get("/:id")
@Middleware("middleware", "get")
public get(
@Ctx() ctx: Context,
@Params("id") id: string
): void {
ctx.json({ id, name: "Test2Controller" });
}
}
const main = async () => {
const app = new Application([
new TestController,
new Test2Controller,
], {
path: "/api",
port: 8081,
});
await app.listen();
};
main().catch(console.log);