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

qzx-controller-express

v1.0.0

Published

decorators based on express and typescript

Downloads

2

Readme

基于 Express + Typescript 的注解式插件

安装

npm i qzx-controller-express --save

使用

import { RouterControllerPlugin } from 'qzx-controller-express';
import { join } from 'path';
app
.use...
.use(
  RouterControllerPlugin({
    hotreload: core.$env.isDevelopment(), // 表示可以不重启加载路由文件 开发环境下使用
    routerPath: join(__dirname, 'routers'), // 表示路由的存放位置,后期会根据这个位置和请求去自动定位处理文件
  }),
);

思想

http的请求多种多样,一般来讲我们都是在路由里面通过路径去匹配。这种方式简单。

然而,随着项目的复杂度的增加,这种方式的可维护性就会下降很多,开发经常性的会遇到一个问题就是不知道去哪里找对应的处理请求。

很多时候,越是复合直觉的其实越简单

在这个基础上,阿里的egg.js框架就提出了,约定往往更加重要,阿里的约定就是各种 controller/service 自动装载。

不过,自动装载其实有点浪费,需要一次性加载所有的路由文件。

按路径加载

我们的这个插件做了一些改变,不是自动装配,也不是在文件里写死路由,而是直接通过请求路径去匹配请求。这很符合 rest 请求的第一直觉。每个路径就相当于一个资源,一个屋子,一个文件夹。

---src
  ---routers
    ---test.ts

假设有如上的一个文件: test.ts, 你可以这么写

import { Get, Query, Param, Controller } from '../../src';
import { ServiceOfCore } from '../../services';

@Controller()
export default class {
  constructor(private $core: ServiceOfCore) {}
  @Get('aaa')
  index() {
    return 'bbb';
  }
  @Get()
  ttt(@Query('name') name: string = '', @Body('sss') ss: string = '') {
    return `${name}${sss}` || 'ccc';
  }
  @Get('asd/:name/dd/:id')
  sss(@Param('name') name: string, @Param('id') id: string) {
    console.log(name, id);
    return `${id}: ${name}`;
  }
}

GET /test/ttt => 'ccc'

GET /test/ttt?name=123 => '123'

POST /test/ttt?name=123 body {sss: 1111} => '1231111'

GET /test/aaa => 'bbb'

GET /test/asd/qianzhixiang/dd/1 - 1: qianzhixiang