Git Product home page Git Product logo

groupguanfang / naily Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 1.0 159 KB

(❗️项目已经迁移到https://github.com/nailyjs/core ,本仓库不再更新)一个minimal的仿Springboot和Nest.js的框架,无聊做一做罢了。实现了依赖注入、控制器、中间件、错误过滤器,管道等仍然在进行中。造轮子嘛…这个轮子没有模块化管理的,依赖注入很自由,随便注入,要咋玩看你自己。我也没想要做啥企业级的啥啥框架,写着玩的嘿嘿嘿~

License: Apache License 2.0

TypeScript 99.59% Shell 0.41%
midway midwayjs nest nestjs nestjs-backend

naily's Introduction

Naily(Nai Core)

❗️项目已经迁移到https://github.com/nailyjs/core,本仓库不再更新,请注意

基于 Express 实现了一个简易的,无模块系统的 Nest.js。

自动依赖注入特性+无模块系统,企业级开发就算啦,写着玩的,代码写得很垃圾。

使用

只支持 TypeScript,没去特别适配 JavaScript 和 Babel 转译器。如果使用有问题别问我(

安装

需有node^12 + pnpm环境,然后运行 cli 命令:

pnpx naily

一个简单的小示例

main.ts

// 注入你写的控制器
// 必须在导入app对象之前导入 才能被检测到并挂载
import "./main.controller";
// 所有控制器导入之后再导入此装饰器!
import { BootNailyApplication, type CanBoot, IMount } from "../src/app";

@BootNailyApplication
export class Booter implements CanBoot {
  // 实现了main函数
  main(app: IMount): void {
    app.boot(8000);
  }
}

app
  // 使用useMiddleware创建中间件
  .useMiddleware((req, res, next) => {
    // 这里编写您的中间件...
  })
  // 使用useFilter将在全局挂载Filter
  .useFilter(MyFilter)
  // 最后使用boot启动服务器
  .boot(8000);

main.controller.ts

// 导入控制器注解和GET方法注解
import { GetMapping, RequestIp, RestController } from "naily";
// 导入一个服务
import { AppService } from "./main.service";

// 这里导出一个类 用controller装饰起来~
@RestController()
export class AppController {
  // 这里注入一个服务~
  constructor(private readonly appService: AppService) {}
  // 这里用get装饰起来~
  @GetMapping()
  // 使用@RequestIp可以获取到请求发出地的IP地址哦
  public getHello(@RequestIp ip: string) {
    // 返回一个JSON对象吧
    return {
      ip: ip,
      data: this.appService.getData(),
      message: "Hello world",
    };
  }
}

main.service.ts

import { Injectable } from "naily";

// 使用Injectable标记这是一个可以被注入的类
// 说人话就两个字:服务
@Injectable
export class AppService {
  // 随便弄个方法
  getData() {
    return 200;
  }
}

装饰器列表

装饰器 说明
RestController 控制器 标识一个控制器必须要用这个
GetMapping 标识 GET 方法装饰器
PostMapping 标识 POST 方法装饰器
OptionsMapping 标识 Options 方法装饰器
PutMapping 标识 Put 方法装饰器
PatchMapping 标识 Patch 方法装饰器
DeleteMapping 标识 Delete 方法装饰器
RequestMapping 匹配所有方法装饰器
RequestQuery 获取 URL Query 装饰器
RequestParam 获取 URL Param 装饰器
RequestBody 获取 URL Body 装饰器
RequestIp 获取 Ip 地址 装饰器
UseFilter 使用错误过滤器 |

错误过滤器

参考test/main.filter.ts

感谢

naily's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

misterma77

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.