新手入门Nest.js(十三)- Providers - Services
Providers是什么,看下官网的解释
Providers are a fundamental concept in Nest. Many of the basic Nest classes may be treated as a provider – services, repositories, factories, helpers, and so on. The main idea of a provider is that it can inject dependencies; this means objects can create various relationships with each other, and the function of "wiring up" instances of objects can largely be delegated to the Nest runtime system. A provider is simply a class annotated with an
@Injectable()
decorator.
Services
通过命令行创建 Services
nest g service cats
执行后结果类似如下
$ nest g service cats
CREATE src/cats/cats.service.spec.ts (446 bytes)
CREATE src/cats/cats.service.ts (88 bytes)
UPDATE src/app.module.ts (544 bytes)
创建完src/cats/cats.service.ts
代码如下
import { Injectable } from '@nestjs/common';
@Injectable()
export class CatsService {}
修改下,修改前提我们添加一个interface
如果创建interface
,命令行命令如下
$ nest g interface cat
CREATE src/cat.interface.ts (24 bytes)
创建完src/cat.interface.ts
代码如下
export interface Cat {}
修改后src/cats/cats.service.ts
的代码如下
import { Injectable } from '@nestjs/common';
import { Cat } from 'src/cat.interface';
@Injectable()
export class CatsService {
private readonly cats: Cat[] = [];
create(cat: Cat) {
this.cats.push(cat);
}
findAll() {
return this.cats;
}
}
顺便修改src/cat.interface.ts,代码如下
export interface Cat {
name: string;
age: number;
bread: string;
}
下面看下如何在Controller中应用
constructor(private catsService: CatsService) {}
@Get()
async findAll(@Req() request: Request): Promise<Cat[]> {
return this.catsService.findAll();
}
@Post()
async create(@Body() createCatDto: CreateCatDto) {
this.catsService.create(createCatDto);
console.log(createCatDto);
return 'This action will create a new cat';
}
上面代码是主要关注的地方
另外需要导入对应的包
import { Cat } from 'src/cat.interface';
import { CatsService } from './cats.service';
然后访问http://127.0.0.1:3000/cats
会得到一个空数组
执行下面的命令
$ curl -d 'name=durban1&age=12&bread=1' http://127.0.0.1:3000/cats
然后在访问http://127.0.0.1:3000/cats
类似如下的数据
[{"name":"durban1","age":"12","bread":"1"},{"name":"durban1","age":"12","bread":"1"}]
版权声明
由 durban创作并维护的 Gowhich博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于 博客( https://www.gowhich.com ),版权所有,侵权必究。
版权声明
由 durban创作并维护的 Gowhich博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于 Gowhich博客( https://www.gowhich.com ),版权所有,侵权必究。