An easy-to-use framework base on Typescript
Umajs is a lightweight Node.js Web framework based on Typescript launched by 58.com. Its Chinese meaning is Ursa Major, and the Big Dipper is part of it; just as Umajs is also composed of different packages. We hope that every part of Umajs is excellent, shining, and can withstand the test of various large-scale projects.
- Based on Koa2, compatible with middleware.
- Unified Response (Result), making it easier and clearer for Controller to respond with data.
- Decorators, making code organization more elegant and convenient.
- Custom Decorators (createArgDecorator) can quickly extend argument decorators for parameter validation, transformation, aggregation, etc.
- Aspect-Oriented Programming (AOP), decorator-based
Aspectcan be easily used to intercept and modify arbitrary methods, and can convert middleware intoAspect.aroundusage. - Dependency Injection (IOC), module dependencies no longer need to be imported and instantiated manually.
- Plugins and Aspects make middleware usage more elegant.
- High stability and high performance, with full unit test coverage.
- Route layering, optimizing route addressing.
- TS development is recommended, making development and maintenance clearer, and usage of dependency injection methods more convenient.
- Unified constraints and specifications.
Please refer to: Umajs Documentation
Backup site [Recommended]: https://umajs.gitee.io/
yarn install
yarn run init
cd packages/app && npm run app
-
When developing a sub-project, run in the sub-project root directory:
npm start -
To start the web project, run in the app project root directory:
npm run app -
To start debugging mode, just press F5 in the root directory.
Welcome to submit PRs or Issues to provide suggestions and feedback.
The following code demonstrates the following features:
Create argument decorators via
createArgDecoratorto validate and transform theageparameter.The controller easily responds with data via
Resultprovided by the framework.
Aspectintercepts methods via thearoundmethod, validating methodargumentsand validating/modifyingreturn values.
// index.controller.ts
import Method from './method.aspect';
import { AgeCheck } from './ArgDecorator';
@Aspect(Method) // Can decorate the class to decorate all methods
export default class extends BaseController {
// @Aspect(Method) // Can decorate a method to decorate a single method
@Path('/hello')
index(@Query('name') name: string, @AgeCheck('age') age: number ) {
return Result.json({
name,
age,
});
}
}// ArgDecorator.ts
export const AgeCheck = createArgDecorator((ctx: IContext, ageKey: string) => {
let age = ctx.query[ageKey];
if (age === undefined) return Result.json({
code: 0,
msg: 'Please add age parameter',
});
age = +age;
if (Number.isNaN(age) || age < 0 || age > 120) return Result.json({
code: 0,
msg: 'Please pass correct age parameter',
});
return age;
});// method.aspect.ts
import { IAspect, IProceedJoinPoint } from '@umajs/core';
export default class implements IAspect {
async around(proceedPoint: IProceedJoinPoint) {
const { proceed, args } = proceedPoint;
// Validate arguments
if (args[0] !== 'Umajs') return Result.send('name must be Umajs');
const result = await proceed(...args);
if (result.type === 'json') {
// Add timestamp to JSON return value
result.data.time = new Date();
}
return result;
}
}