1.0.34 • Published 5 years ago

ts-decorator-express v1.0.34

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

typadaExpressDecorator

What is it ?

  • Typescript + ES6 + Decorator + Express = ts-decorator-express

Feature

  • Method Decorator( Get, Put, Post, Delete, Patch ) + Method Level Middleware
  • Controller Decorator + Controller Level Middleware
  • Application Level Middleware
  • Required Parameter( >= 1.0.21)
  • Required Channing ( >= 1.0.28)
  • Regexp Route ( >= 1.0.32)

Getting Start

npm i -D express@4 @types/express typecript
npm i ts-decorator-express

express 4 >=, typescript 2 >=, node 10 >=

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "lib": [
            "es2015",
        ],
        "types": ["reflect-metadata"],
        "module": "commonjs",
        "baseUrl": "./",
        "strictNullChecks": true,
        "moduleResolution": "node",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
    },
    "exclude": [
        "node_modules"
    ]
}

Example

// ./server.ts

import './src/controllers';
import { TypadaExpressInstance } from "ts-decorator-express";
import * as Express from 'express';

const app = TypadaExpressInstance.createInstance([
    Express.json(), Express.urlencoded({ extended: true }),
]);

app.listen(3001, () => {
    console.log('Typada Express Decorator Start, ', 3001);
});
// ./src/controllers/users
import { Controller, Get } from "ts-decorator-express";
import { middle1, middle2, middle3 } from "../middlewares";

@Controller('/users', middle1, middle2, middle3)
export class User {
    
    @Get('', middle1, middle2 ,middle3)
    async getUsers(req, res, next) {
        try {
            console.log('get Users')
            return res.status(200).json({
                status:200
            })
        } catch (error) {
            console.log(error);
        }
    }
    
    @Get(/\d/)
    async getUserById(req, res, next) {
        try{
         	console.log('get User By Id');
            return res.status(200).json({
                status:200
            })            
        } catch (error) {
            console.log(error);            
        }        
	}
    
    @Post('')
    async createUser(@Required.Body(['password']).Query(['id', 'email']) req, res, next) {
        try {
            console.log('create User')
            return res.status(200).json({
                status:200
            })
        } catch (error) {
            console.log(error);
        }
    }    
}
// ./src/controllers/index
import './user';
import { Request, Response, NextFunction } from "express";

export const middle1 = (req: Request, res: Response, next: NextFunction) => {
    try {
        console.log('middle1');
        next();
    } catch (error) {
        console.log(error);
    }
}
export const middle2 = (req: Request, res: Response, next: NextFunction) => {
    try {
        console.log('middle2');
        next();
    } catch (error) {
        console.log(error);
    }
}
export const middle3 = (req: Request, res: Response, next: NextFunction) => {
    try {
        console.log('middle3');
        next();
    } catch (error) {
        console.log(error);
    }
}
export const middle4 = (req: Request, res: Response, next: NextFunction) => {
    try {
        console.log('middle4');
        next();
    } catch (error) {
        console.log(error);
    }
}

Result

http://localhost:3001/users

Versioning

  • v1.0.32
    • add Regexp Route
  • v1.0.28
    • add required Chaning
  • v1.0.20
    • required decorator
  • v1.0.15
    • proxy instance
  • v1.0.10
1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago