npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-action

v2.0.7

Published

web framework for koa2

Downloads

10

Readme

koa-action 2.0.0

koa2 mvc framework easy to use

Getting Started

npm install

npm install koa-action

yarn install

yarn add koa-action

##make dirs

|-- src
    |-- controllers		// controller目录
    |-- entries			// typeorm entry 目录
    |-- interceptors
    |-- po				// Pojo Beans
    |-- services		// services 
    |-- config.ts		// 配置文件
    |-- index.ts		// APP file

index.ts

import './config';
import { KoaAction, ScanPath, Log, ScanAspects } from '../koa-action';
import { AuthInterceptor } from './interceptors/AuthInterceptor';

@ScanPath('./src/controllers')
@ScanAspects('./src/aspects')
class Service extends KoaAction{
	@Log()
	private log: any;

	constructor () {
		super();

		this.init();
	}

	init () {
		this.registerInterceptor(new AuthInterceptor())
			.run();
	}
}
const s = new Service();

//run
npm run dev

config.ts

import { Global } from "../koa-action/Global";

Global.config = {
	serviceName: 'xxx',
	port: 18080,
	upload: 'uploads',
	dataSource: {
		type: "mysql",
		host: "localhost",
		port: 3306,
		username: "test",
		password: "test",
		database: "test",
		entities: ['src/**/*.entity{.js,.ts}'],
		synchronize: false,
		logger: undefined, // 'advanced-console',
		logging: false //'all',
	},
	redis: {
		host: '127.0.0.1',
		port: 6379,
		password: '',
		db: 2
	},
	redisSession: {
		key: 'ka.sid',
		ttl: 5 * 60 * 1000  // 5分钟
	},
	logger: {
		name: 'fqbd',
		numBackups: 15,
		pattern: '[%d{yyyy-MM-dd hh:mm:ss}] [pid:%z] [%p] [%C.%M] %c - %m',
		path: path.resolve(process.cwd(), 'logs', 'logs.log'),
		level: 'info',
		pm2: true,
		pm2InstanceVar: 'INSTANCE_ID'
	}
};

decorators

框架内置多个装饰器方便进行web开发

@ScanPath('path')

类装饰器 指定controller路径

@ScanPath('./src/controllers')
class Service extends KoaAction{}

@ScanAspects('path')

类装饰器 指定AOP路径

@ScanAspects('./src/aspects')
class Service extends KoaAction{}

@Controller('/path')

类装饰器 指定模块路由

@Controller('/admin')
class AdminController {}

@Service

类装饰器 注册Service类型的实例,无传值

@Service
export class AdminService {}

@Log()

属性装饰器 注入日志,对应的对象只初始化一次

@Log()
private log: any;

@Autowired

属性装饰器 注入该类型的对象,对应的对象只初始化一次, 无传值

@Autowired
private userDao: UserDao;

@Get

函数装饰器method 为GET的路由

@Get('/list')
async list (@Param('name') a: string, @Headers headers: any):Promise<DataResponse> {}

@Post

函数装饰器method 为POST的路由

@Post('/add')
async add (@Body user: User):Promise<DataResponse> {}

@Put

函数装饰器method 为Put的路由

@Delete

函数装饰器method 为Delete的路由

@Head

函数装饰器method 为Head的路由

@All

函数装饰器method 为All的路由

@Status(200)

函数装饰器 指定要返回的状态码

@ContentType('mimeType')

函数装饰器 指定要返回的content-type

@Param('name')

属性装饰器 根据name属性获取url中的参数

@Get('/list')
async list (@Param('name') a: string, @Headers headers: any):Promise<DataResponse> {}

@Headers

属性装饰器 获取request中的headers信息

@Get('/list')
async list (@Param('name') a: string, @Headers headers: any):Promise<DataResponse> {}

@Body

属性装饰器 获取上传的Body中的信息

@Post('/login')
async login (@Body user: User, @Context ctx: any):Promise<DataResponse> {}

@Context

属性装饰器 获取上下文对象

@Post('/login')
async login (@Body user: User, @Context ctx: any):Promise<DataResponse> {}

@File

属性装饰器 获取上传文件参数

@Post('/upload')
async upload (@File('file') file: any, @Context ctx: any):Promise<DataResponse> {}

@Request

属性装饰器 获取ctx.request对象

@Response

属性装饰器 获取ctx.response对象

@Cookies

属性装饰器 获取cookies对象

@Cookie('key')

属性装饰器 获取cookies对象中指定的参数

@Session

属性装饰器 获取session对象

@SessionParam('key')

属性装饰器 获取session对象中的属性

@Auth

属性装饰器 获取授权信息

@Validate

属性装饰器 对属性进行校验,使用class-validator组件

@Post('/add')
async add (@Validate @Body a: User):Promise<DataResponse> {}

其他可以使用的业务模块

定时任务 agenda 消息队列 bull