该问题通常是由于 API Gateway 和 Lambda 函数配置的不一致导致的。需要检查 API Gateway 中的路径和 Lambda 函数中的路径是否匹配,还要检查 API Gateway 中的 HTTP 方法和 Lambda 函数中的方法是否匹配。若使用 NestJS 搭建 API,则需要在 main.ts 中指定 API 路径,并使用 @nestjs/common 包中的 NotFoundException 抛出异常。
具体代码示例如下:
在 main.ts 中指定 API 路径:
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api'); // 设置全局前缀为api
await app.listen(3000);
在 controller 中使用 NotFoundException:
import { NotFoundException } from '@nestjs/common';
@Controller('users')
export class UsersController {
@Get(':id')
findOne(@Param('id') id: string) {
const user = findUserById(id);
if (!user) {
throw new NotFoundException('User not found');
}
return user;
}
}