A Web framework For Deno with Decorator and middleware

A Web framework For Deno with Decorator and middleware

leaves - Web framework For Deno with Decorator and middleware

leaves - Web framework For Deno with Decorator and middleware

Demo - test/

Controller.ts

import {
  Controller,
  Get,
  Post,
  Req,
  RequestBody,
  RequestParam,
  Res,
  Validate,
} from "../deps.ts";
import { LogMiddleware } from "../middleware/Log.ts";
import { Authorize } from "../middleware/Authorize.ts";
import { FooService } from "../service/foo.service.ts";
import { User } from "../model/User.ts";

@Controller("/app")
export class TestCtrl {
  constructor(private service: FooService) {
  }

  @Authorize
  @LogMiddleware
  @Get("/test/:id")
  async testHandle(@RequestParam id: number, req: Req, res: Res) {
    console.log("name", this.service.getName());
    return this.service.getName();
  }

  @Validate
  @Post("/testPost/:id")
  async postHandle(@RequestBody(User) person: User, req: Req, res: Res) {
    console.log("person", person);
    return { msg: "ok", status: 0 };
  }
}

Server.ts

import { TestCtrl } from "./controller/TestCtrl.ts";
import { MiddlewareGlobal } from "./middleware/Middleware.ts";
import { App, Res, transformer } from "./deps.ts";

const { plainToClass } = transformer;

const app = new App([TestCtrl], [MiddlewareGlobal]);
// global error handler
app.error((res: Res, error: Error) => {
  res.body = "This page unprocessed error: " + error.message;
  res.status = 500;
});
// global body transformer
app.useTransform({
  type: "body", // parse body params
  getTransform: (transform: any, body: any) => {
    return plainToClass(transform, body);
  },
});
app.listen(
  { port: 8000, hostname: "127.0.0.1" },
);

tsconfig.app.json:

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    }
}

Run:

deno run -c tsconfig.json --allow-net --allow-read --unstable app.ts

Notice:

Custom Middleware

Log.ts

import { defineMiddleware, Req, Res } from "../../mod.ts";

export const LogMiddleware = defineMiddleware(
  async (req: Req, res: Res, next: Function) => {
    console.log("log1");
    await next();
    console.log("log1 end");
  },
);

Controller.ts

@LogMiddleware
@Get("/test/:id")
async testHandle(@RequestParam id: number, req: Req, res: Res) {
  console.log("name", this.service.getName());
  // throw new Error("tt");
  return this.service.getName();
}

Download Details:

Author: tuhe32

Source Code: https://github.com/tuhe32/leaves

deno node nodejs javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

AEM Querybuilder for JavaScript (Browser, Node, Deno)

Helix Querybuilder .AEM Querybuilder for JavaScript (Browser, Node, Deno)

NKeys for JavaScript - Node.js, Browsers, and Deno

A public-key signature system based on Ed25519 for the NATS ecosystem system for JavaScript. The nkeys.js library works in Deno, Node.js, and the browser!

Javascript module for easily generating mazes,compatible with both Deno and Node

A work-in-progress Javascript maze generator module, compatible with both Deno and Node.

Deno Tutorial for Beginners: Deno vs. Node: Better Than NodeJS?

Deno is released, it’s time to find out if it can prove a worthy competition for Node.js. What is Deno? Why Deno can be considered an alternative to Node? The main differences between Deno and Node. Write a simple Deno app.

What is Deno? The Node.JS Killer? The Deno-minator to Node?

What is Deno? The Node.JS Killer? The Deno-minator to Node? Deno is the new hype and will it be better than node.js? Deno is a secure runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language that addresses Node’s shortcomings