https://github.com/0261/typadaexpressdecorator
Typescript Class, Method Decorator for Exressjs using reflectMetadata
https://github.com/0261/typadaexpressdecorator
decorator decorators es6 es7 express expressjs javascript middleware proxy reflect typscript
Last synced: 2 months ago
JSON representation
Typescript Class, Method Decorator for Exressjs using reflectMetadata
- Host: GitHub
- URL: https://github.com/0261/typadaexpressdecorator
- Owner: 0261
- Created: 2019-04-09T02:07:18.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-10-30T14:03:08.000Z (over 6 years ago)
- Last Synced: 2025-03-14T05:04:23.959Z (about 1 year ago)
- Topics: decorator, decorators, es6, es7, express, expressjs, javascript, middleware, proxy, reflect, typscript
- Language: TypeScript
- Size: 90.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## 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
```sh
npm i -D express@4 @types/express typecript
npm i ts-decorator-express
```
> `express` 4 >=, `typescript` 2 >=, `node` 10 >=
### tsconfig.json
```json
{
"compilerOptions": {
"target": "es5",
"lib": [
"es2015",
],
"types": ["reflect-metadata"],
"module": "commonjs",
"baseUrl": "./",
"strictNullChecks": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
},
"exclude": [
"node_modules"
]
}
```
## Example
```typescript
// ./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);
});
```
```typescript
// ./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);
}
}
}
```
```typescript
// ./src/controllers/index
import './user';
```
```typescript
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
+