diff --git a/package.json b/package.json index 1191a8f..f6c8a4f 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "@nestjs/common": "^9.0.0", "@nestjs/core": "^9.0.0", "@nestjs/platform-express": "^9.0.0", + "nanoid": "^4.0.0", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.2.0" diff --git a/src/app.controller.ts b/src/app.controller.ts index 36f9690..a9a21be 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -1,16 +1,21 @@ -import { Controller, Get } from '@nestjs/common'; +import { Controller, Get, Param } from '@nestjs/common'; import { AppService } from './app.service'; @Controller() export class AppController { - constructor(private readonly appService: AppService) {} + constructor(private readonly appService: AppService) { } @Get() getHello(): string { return this.appService.getHello(); } @Get('/list') - getSwipList():any[]{ + getSwipList(): any[] { return this.appService.getSwipList(); } + @Get(':id') + // @Param('id') 有参数的话,返回的就是没有被序列化的值 --> 没有参数的话,会自动序列化 + say(@Param('id') id: string): string { + return id + } } diff --git a/src/app.module.ts b/src/app.module.ts index 8662803..fa4c0f7 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,10 +1,15 @@ -import { Module } from '@nestjs/common'; +import { MiddlewareConsumer, Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; +import { CatModule } from './cat/cat.module'; @Module({ - imports: [], + imports: [CatModule], controllers: [AppController], providers: [AppService], }) -export class AppModule {} +export class AppModule { + // configure(consumer:MiddlewareConsumer){ + // consumer.apply().forRoutes() + // } +} diff --git a/src/app.service.ts b/src/app.service.ts index a8570d8..d9ffbda 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -1,26 +1,30 @@ import { Injectable } from '@nestjs/common'; interface Swipe { - name:string, - imgUrl:string + uuid: number, + imgUrl: string } @Injectable() export class AppService { getHello(): string { return 'Hello World!'; } - getSwipList():Swipe[]{ + getSwipList(): Swipe[] { const imgList = [ { - name:"第一张数据11", - imgUrl:"test11" + uuid: Math.random() * 6, + imgUrl: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp09%2F210F2130512J47-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665481740&t=0bfa9f3d1f071e8381f14a1e858fa2d6" }, { - name:"第一张", - imgUrl:"test" + uuid: Math.random() * 6, + imgUrl: "https://img1.baidu.com/it/u=1546227440,2897989905&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" }, { - name:"第一张", - imgUrl:"test" + uuid: Math.random() * 6, + imgUrl: "https://img0.baidu.com/it/u=747797291,1321739150&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" + }, + { + uuid: Math.random() * 6, + imgUrl: "https://img0.baidu.com/it/u=3643895624,2552772604&fm=253&fmt=auto&app=120&f=JPEG?w=1200&h=675" }, ] return imgList diff --git a/src/cat/cat.controller.spec.ts b/src/cat/cat.controller.spec.ts new file mode 100644 index 0000000..e4473fb --- /dev/null +++ b/src/cat/cat.controller.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { CatController } from './cat.controller'; + +describe('CatController', () => { + let controller: CatController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [CatController], + }).compile(); + + controller = module.get(CatController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/src/cat/cat.controller.ts b/src/cat/cat.controller.ts new file mode 100644 index 0000000..a6087ce --- /dev/null +++ b/src/cat/cat.controller.ts @@ -0,0 +1,5 @@ +import { Controller } from '@nestjs/common'; + +@Controller('cat') +export class CatController { +} \ No newline at end of file diff --git a/src/cat/cat.module.ts b/src/cat/cat.module.ts new file mode 100644 index 0000000..801470b --- /dev/null +++ b/src/cat/cat.module.ts @@ -0,0 +1,7 @@ +import { Module } from '@nestjs/common'; +import { CatController } from './cat.controller'; + +@Module({ + controllers: [CatController] +}) +export class CatModule {} diff --git a/src/main.ts b/src/main.ts index 13cad38..58b7edd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,7 @@ import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); - await app.listen(3000); + app.enableCors();//node设置跨域 + await app.listen(5000); } bootstrap();