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

easy-node-ioc

v1.1.2

Published

Typescript IOC Tool

Downloads

4

Readme

easy-node-ioc

用 Typescript 的装饰器实现依赖注入,就像我们使用 Spring MVC 框架一样,web 框架使用的是 Express

Version npmnpm Downloads

安装

npm i easy-node-ioc --save-dev

快速开始使用

git clone https://github.com/chenkang084/easy-node-ioc.git
npm i
NODE_ENV=development npx ts-node demo/App.ts

执行完以上命令,将在命令行输出 Example app has started,代码项目已正常经启动起来了,尝试访问 http://localhost:9001/api/test/index ,页面将返回 OK。

使用

1.创建 Controller

import { Controller} from 'easy-node-ioc';
@Controller('/test')
class TestControl {
    ...
}

2.创建 Service

import { Service } from 'easy-node-ioc';
@Service('')
class TestService {
    ...
}

3.将 Service 注入到 Controller 中

import { Autowired,Controller } from 'easy-node-ioc';
@Controller('/test')
class TestControl {
    @Autowired
    testService: TestService;
    ...
}

4.在 Controller 中定义 Rest API,例如 GET,POST,PUT,DELETE,PATCH

import { Autowired,Controller,GET,RequestParam } from 'easy-node-ioc';
@Controller('/test')
class TestControl {
    @Autowired
    testService: TestService;
    @Get('/index')
    index(@RequestParam('age') age: number, req: Request, res: Response) {
        console.log('index method');
        this.dbService.queryDb();

        res.status(200).send(this.testService.queryDb());
    }
    ...
}

5.定义 App,添加 ComponentScan,添加 Bootstrap 注解到 App 类

import { Bootstrap, ComponentScan } from '../';
@ComponentScan(join(__dirname, './Controller.ts'))
@Bootstrap
class App {
  constructor() {}

  app = express();

  main() {
    const server = http.createServer(this.app);

    server.listen(9001, function () {
      console.log('Example app listening at http://%s:%s');
    });
  }
}

第 5 步是非常关键的,ComponentScan 注解负责告诉easy-node-ioc去指定目录读取 js/ts 文件,在读取文件的过程中,根据 Decorator 定义,向容器中添加对应实例,在 Boostrap 方法里面根据文件依赖,去容器中获取已经实例化的对象(如果对象没有实例化,就立即实例化),等所有的依赖都注入完成,执行main方法。

测试

npm test 本项目已经写了一些基础的 test case,可以在项目路径下的 tests 目录查看。

Debug

.vscode目录的 launch.json 文件中,已经配置好了 debug 相关的代码,你可以直接在vscode中使用 F5 进行 debug,这样更方便你了解项目是如何实现的。

其他

如果你对decorator比较感兴趣,可以查看相关资料,了解 decorator 如何使用。

我建立了一个微信群,如果你对这个小工具感兴趣,可以加群,或者如果你有什么问题,也可以进群交流。

TODO

用 uuid 作为 key,重新定义 iocContainer ,重新组织 key --> instance