为了添加http://localhost:8080/api/v1/admin(Get)来开发与NestJS的接口,我们添加了Get注解,并继续进行接口开发,但接口没有Map,如果调用该接口,会出现404Not Found错误。什么样的问题?
//admin.Controler.ts
import { Body, Controller, Get, HttpCode, Post, UseGuards } from '@nestjs/common';
import {
ApiTags,
ApiOperation,
ApiResponse,
} from '@nestjs/swagger';
import { ReqAdmin } from 'src/lib/decorator/req-admin.decorator';
import { HttpAuthGuard } from 'src/guards/auth/http-auth.guard';
import { BaseResponse } from 'src/models/http/base.response';
import { AdminService } from './admin.service';
import { CreateAdminDto } from './dto/create.admin.dto';
import { LoginAdminDto } from './dto/login.admin.dto';
import { Admin } from 'src/entities/admin.entity';
@Controller('admin')
@ApiTags('관리자 API')
export class AdminController {
constructor(
private readonly adminService: AdminService
) {}
@Get('/')
@UseGuards(HttpAuthGuard)
@ApiOperation({ summary: '내 정보 보기', description: '내정보 보기' })
@ApiResponse({ status: 200, description: '내 정보 조회 성공' })
public async findOne(@ReqAdmin() admin: Admin): Promise<BaseResponse> {
const data = await this.adminService.findOneById(admin.id);
return BaseResponse.object('내정보 조회 성공', {
data,
})
}
@Post('/')
@ApiOperation({ summary: '관리자 생성 API', description: '관리자를 생성한다.' })
@ApiResponse({ status: 201, description: '관리자 생성성공' })
public async create(@Body() createAdminDto: CreateAdminDto) {
await this.adminService.create(createAdminDto);
return BaseResponse.object('관리자 생성성공')
}
@Post('/login')
@HttpCode(200)
@ApiOperation({ summary: '관리자 로그인 API', description: '관리자 로그인' })
@ApiResponse({ status: 200, description: '관리자 로그인성공', type: String })
public async login(@Body() loginAdminDto: LoginAdminDto) {
const token = await this.adminService.login(loginAdminDto);
return BaseResponse.object('관리자 로그인성공', {
'x-access-token': token
})
}
}
//admin.mode.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Admin } from 'src/entities/admin.entity';
import { TokenModule } from 'src/token/token.module';
import { TokenService } from 'src/token/token.service';
import { AdminController } from './admin.controller';
import { AdminRepository } from './admin.repository';
import { AdminService } from './admin.service';
@Module({
imports: [
TypeOrmModule.forFeature([
Admin
])
],
providers: [AdminService, TokenService],
exports: [AdminService],
controllers: [AdminController]
})
export class AdminModule {}
//终端
iwonje@iwonje-ui-MacBookAir mcn-admin-backend % nest start
[Nest] 40115 - 2022. 10. 11. 오후 4:12:15 LOG [NestFactory] Starting Nest
application...
[Nest] 40115 - 2022. 10. 11. 오후 4:12:15 LOG [InstanceLoader] AppModule
dependencies initialized +42ms
[Nest] 40115 - 2022. 10. 11. 오후 4:12:15 LOG [InstanceLoader] TypeOrmModule
dependencies initialized +0ms
[Nest] 40115 - 2022. 10. 11. 오후 4:12:15 LOG [InstanceLoader] TypeOrmCoreModule
dependencies initialized +303ms
[Nest] 40115 - 2022. 10. 11. 오후 4:12:15 LOG [InstanceLoader] TypeOrmModule
dependencies initialized +0ms
[Nest] 40115 - 2022. 10. 11. 오후 4:12:15 LOG [InstanceLoader] AdminModule
dependencies initialized +1ms
[Nest] 40115 - 2022. 10. 11. 오후 4:12:15 LOG [RoutesResolver] AdminController
{/api/v1/admin}: +287ms
[Nest] 40115 - 2022. 10. 11. 오후 4:12:15 LOG [RouterExplorer] Mapped
{/api/v1/admin, POST} route +1ms
[Nest] 40115 - 2022. 10. 11. 오후 4:12:15 LOG [RouterExplorer] Mapped
{/api/v1/admin/login, POST} route +1ms
[Nest] 40115 - 2022. 10. 11. 오후 4:12:15 LOG [NestApplication] Nest application
successfully started +1ms
//app.mode.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { DatabaseTypes } from '../database/database.enum';
import { DatabaseFactory } from '../database/database.factory';
import { AdminModule } from 'src/admin/admin.module';
@Module({
imports: [
TypeOrmModule.forRoot(
DatabaseFactory.createDatabase(DatabaseTypes.MYSQL).options(),
),
AdminModule
],
})
export class AppModule {}
1条答案
按热度按时间oxcyiej71#
看起来您已经为您的API
/api/v1
添加了前缀您可以尝试调用http://localhost:8080/api/v1/admin而不是http://localhost:8080/admin