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 🙏

© 2025 – Pkg Stats / Ryan Hefner

decorator-controller

v1.0.8

Published

使用ES7新特性的修饰器实现控制层,ES6环境需要babel支持,暂时不支持ES5

Readme

Decorator Controller

项目简介:

  使用ES7的新特性——修饰器(decorator)实现的一个NodeJS控制层框架。通过递归扫描目标目录,解析注解实现控制层。你可以用一个@RequestMapping注解简单地实现一个请求接口,使用起来就和SpringMVC一样。告别繁琐又麻烦地app.get和app.post的使用,拒绝像express-controller用下划线写请求接口,用数组写中间件。你可以自由使用注解声明各种接口,你也可以自定义中间件,使用注解的方式,决定哪一个接口使用中间件。  由于当前版本的NodeJS尚未完全实现对ES7的支持,因此,使用时需要配合babel使用transform-decorators-legacy插件进行转码。

安装

npm i decorator-controller

使用

安装babel

安装babel相关依赖和ES7修饰器特性转码包 babel-plugin-transform-decorators-legacy

npm i babel-cli --save
npm i babel-core --save
npm i babel-register --save
npm i babel-plugin-transform-decorators-legacy --save

在项目根目录下面创建一个.babelrc,内容如下:

.babelrc

{
  "plugins": [
    "transform-decorators-legacy"
  ]
}

个人开发可以引入babel-register实现实时转码,内容如下:

babel-app.js

/* 由于修饰器属于ES7的特性,ES6要支持的话,需要用babel进行转码 */
require('babel-register')
require('./app')

引入babel-register后,会对后面require进来的文件进行转码。

app.js

const http = require('http')
const path = require('path')
const express = require('express')
const bodyParser = require('body-parser')
// 这里引入decorator-controller
const controller = require('decorator-controller')

const app = express()
app.use(bodyParser.urlencoded({ extended: false }))

// 扫描src目录下的js文件,根据注解创建控制层
controller.scan(path.join(__dirname, './src')).bind(app)

const server = http.createServer(app)
server.listen(process.env.PORT || '3000', function () {
  let host = server.address().address
  let port = server.address().port
  console.log('server listening at http://%s:%s', host, port)
})

这里实现了一个简单的控制器

UserController.js

// 引入各种注解
const { Controller, RequestMapping, RequestMethod } = require('decorator-controller/annotation')

// 声明控制器
@Controller
// 声明控制器请求前缀,可以不写
@RequestMapping('/user')
class UserController {

  // 创建一个get请求  
  @RequestMapping('/getUserName')
  getUserName () {
    return 'username'
  }

  // 创建一个post请求  
  @RequestMapping('/saveUser', RequestMethod.POST)
  saveUser () {
    console.log('saveUser')
    return 'success'
  }
}

module.exports = UserController

运行babel-app.js文件,即可运行