NestJS




A progressive Node.js framework for building efficient and scalable server-side applications on top of TypeScript & JavaScript (ES6 / ES7 / ES8) heavily inspired by Angular

底层用 express

设计理念
这个后端框架受 Angular 启发 , 所以业务代码看起来和 Angular一样
完全基于typescript的web框架,完全oop风格

底层是 express,如果是koa,或者fastify的话,或许会比较火


2018-01-05


官网
https://nestjs.com/


Nest官方叫我们去看Angular 的依赖注入的文档
Nest is built around the strong design pattern commonly known as Dependency Injection. There's a great article about this concept in the official Angular documentation.


根据类型注入 , 不能根据 key

公司




已经很好用了 , 但是不确定是否这么强大 : 高级的功能

官方提供的ORM
分布式微服务

官方有提供 :
命令行工具


示例代码
https://github.com/nestjs/nest.git
E:\NestJS\nest\examples

框架源码
https://github.com/nestjs/nest.git



components 注解相当于 Spring 的 @Service

最后在模块里配置一下 , 相当于Spring 的 xml 配置

import { Module } from '@nestjs/common';
import { CatsController } from './cats/cats.controller';
import { CatsService } from './cats/cats.service'; @Module({
controllers: [CatsController],
components: [CatsService],
})
export class ApplicationModule {}
9
 
1
import { Module } from '@nestjs/common';
2
import { CatsController } from './cats/cats.controller';
3
import { CatsService } from './cats/cats.service';
4

5
@Module({
6
    controllers: [CatsController],
7
    components: [CatsService],
8
})
9
export class ApplicationModule {}



实践

npm start 或者 npm run start
默认在 3000 端口
npm start

> nest-typescript-starter@1.0.0 start E:\NestJS\nest\examples\01-cats-app
> node index.js [Nest] 12736 - 2018-1-5 09:11:29 [NestFactory] Starting Nest application...
[Nest] 12736 - 2018-1-5 09:11:29 [InstanceLoader] ApplicationModule dependencies initialized +7ms
[Nest] 12736 - 2018-1-5 09:11:29 [InstanceLoader] CatsModule dependencies initialized +2ms
[Nest] 12736 - 2018-1-5 09:11:29 [RoutesResolver] CatsController {/cats}: +29ms
[Nest] 12736 - 2018-1-5 09:11:29 [RouterExplorer] Mapped {/, POST} route +1ms
[Nest] 12736 - 2018-1-5 09:11:29 [RouterExplorer] Mapped {/, GET} route +0ms
[Nest] 12736 - 2018-1-5 09:11:29 [RouterExplorer] Mapped {/:id, GET} route +1ms
[Nest] 12736 - 2018-1-5 09:11:29 [NestApplication] Nest application successfully started +1ms
x
 
1
npm start
2

3
> nest-typescript-starter@1.0.0 start E:\NestJS\nest\examples\01-cats-app
4
> node index.js
5

6
[Nest] 12736   - 2018-1-5 09:11:29   [NestFactory] Starting Nest application...
7
[Nest] 12736   - 2018-1-5 09:11:29   [InstanceLoader] ApplicationModule dependencies initialized +7ms
8
[Nest] 12736   - 2018-1-5 09:11:29   [InstanceLoader] CatsModule dependencies initialized +2ms
9
[Nest] 12736   - 2018-1-5 09:11:29   [RoutesResolver] CatsController {/cats}: +29ms
10
[Nest] 12736   - 2018-1-5 09:11:29   [RouterExplorer] Mapped {/, POST} route +1ms
11
[Nest] 12736   - 2018-1-5 09:11:29   [RouterExplorer] Mapped {/, GET} route +0ms
12
[Nest] 12736   - 2018-1-5 09:11:29   [RouterExplorer] Mapped {/:id, GET} route +1ms
13
[Nest] 12736   - 2018-1-5 09:11:29   [NestApplication] Nest application successfully started +1ms

有请求路由的信息 , 比如
/cats



暂时没数据

要发POST请求创建数据 , 角色是 admin , 不然不能添加数据
@UseGuards(RolesGuard)

然后get再获取一次

Guards are executed after every middleware, but before pipes.

为什么要用 Guards ? 之前已经用了 middleware






项目加载顺序
从左到右




NestJS 用TypeScript开发 nodeJS后端的更多相关文章

  1. CabloyJS全栈开发之旅(1):NodeJS后端编译打包全攻略

    背景 毋庸置疑,NodeJS全栈开发包括NodeJS在前端的应用,也包括NodeJS在后端的应用.CabloyJS前端采用Vue+Framework7,采用Webpack进行打包.CabloyJS后端 ...

  2. 使用Visual Studio Code搭建TypeScript开发环境

    使用Visual Studio Code搭建TypeScript开发环境 1.TypeScript是干什么的 ? TypeScript是由微软Anders Hejlsberg(安德斯·海尔斯伯格,也是 ...

  3. 搭建typescript开发环境最详细的全过程

    搭建typescript开发示例https://github.com/Microsoft/TypeScriptSamples typescript案例https://www.tslang.cn/sam ...

  4. TypeScript开发环境搭建(Visual studio code)

    使用Visual Studio Code搭建TypeScript开发环境 1.TypeScript是干什么的 ? TypeScript是由微软Anders Hejlsberg(安德斯·海尔斯伯格,也是 ...

  5. 第二章 TypeScript 开发环境搭建

    Mac OS X 下 TypeScript 开发环境搭建 一.集成开发环境 WebStrom VSCode 二.安装 TypeScript Homebrew(macOS 缺失的软件包管理器) ruby ...

  6. VSCode搭建node + typescript开发环境

    我们一起来喜欢TypeScript 现在写js不用TypeScript,伦家可能会觉得你是外星人. 是的,TypeScript很大程度增强了代码的可读性,可跟踪性,可维护性和减少了bug. 那么没有理 ...

  7. 使用Visual Studio Code + Node.js搭建TypeScript开发环境

    Visual Studio Code搭建Typescript开发环境 —— 相关文章: http://www.cnblogs.com/sunjie9606/p/5945540.html [注意:这里仅 ...

  8. 001——Typescript 介绍 、Typescript 安 装、Typescript 开发工具

    一. Typescript 介绍 1. TypeScript 是由微软开发的一款开源的编程语言. 4. TypeScript 是 Javascript 的超级,遵循最新的 ES6.Es5 规范.Typ ...

  9. 使用 VS Code 搭建 TypeScript 开发环境

    使用 VS Code 搭建 TypeScript 开发环境 TypeScript 是 JavaScript 的超集,TypeScript 只是增强了 JavaScript 而非改变了 JavaScri ...

随机推荐

  1. 部分类Partial

    Partial告诉编译器,一个类,结构,接口的定义源代码可能要分散到一个或者多个源文件中. 在下面的情况下用Partial类型: (1) 类型特别大,不宜放在一个文件中实现.(2) 一个类型中的一部分 ...

  2. 可epoll队列

    什么是可epoll队列? 就可以使用epoll来监控队列中是否有数据的队列,当然也支持select和poll. 应用场景 一个线程,需要将队列(共享内存队列或普通队列均可)中的数据取出来,然后通过网络 ...

  3. Creating Custom UITableViewCells with NIB files

    Maksim Pecherskiy 13 November 2012 Well this sucks. Apparently these days you can only use the Inter ...

  4. C#中的异步调用及异步设计模式(三)——基于事件的异步模式

    四.基于事件的异步模式(设计层面) 基于事件的C#异步编程模式是比IAsyncResult模式更高级的一种异步编程模式,也被用在更多的场合.该异步模式具有以下优点: ·                 ...

  5. idea中java文件打包出去步骤

     打包出去之后 通过xftp放在Linus上面去运行 然后用hadoop fs -put 放到hadoop集群上面(而且此时在/user/hadoop下面创建好input文件夹和一个输入的文件) 即 ...

  6. JDK8新特性:使用stream、Comparator和Method Reference实现集合的优雅排序

    大家对java接口Comparator和Comparable都不陌生,JDK8里面Comparable还和以前一样,没有什么改动:但是Comparator在之前基础上增加了很多static和defau ...

  7. Mac上修改MySQL默认字符集为utf8

    1.检查默认安装的mysql的字符集 mysql> show variables like '%char%'; +--------------------------+------------- ...

  8. RobotFramework做接口自动化(内部接口需要登录token)

    背景: 项目中需要做接口自动化测试,使用robot,有一个收货地址列表的接口直接传参数访问时会返回:{"msg":"缺少参数","code" ...

  9. redis详细说明

    # By default Redis does not run as a daemon. Use 'yes' if you need it.# Note that Redis will write a ...

  10. python学习之路 四 :文件处理

    本节重点 掌握文件的读.写.修改方法 掌握文件的处理模式的区别 一.文件读取 ​    ​1.读取全部内容 # 一次性读取文件 f = open("test.txt",'r',en ...