关于 nestjs
面向 NestJS 开发者的参考技能,适用于构建或重构应用。它按官方文档主题组织示例和脚手架模板,涵盖常见应用模式与集成。
主要功能
- 01按主题整理 NestJS 示例
- 02控制器与模块模式
- 03验证与安全指导
- 04GraphQL 与微服务主题
- 05脚手架模板
适用场景
- 01实现 NestJS 控制器和提供者
- 02添加验证或身份认证
- 03构建 GraphQL 或 WebSocket 功能
- 04编写单元或端到端测试
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ full-stack-skills/ nodejs-skills --skill nestjs安装后在 Claude Code 中输入/nestjs确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
agents/openai.yamlapi/decorators.mdexamples/faq.mdexamples/cli/overview.mdexamples/fundamentals/async-providers.mdcircular-dependency.mdcontrollers.mdcustom-decorators.mdcustom-providers.mddynamic-modules.mdexception-filters.mdexecution-context.mdfirst-steps.mdguards.mdinjection-scopes.mdinterceptors.mdlifecycle-events.mdmiddleware.mdmodule-ref.mdmodules.mdpipes.mdproviders.mdrequest-scoped.mdexamples/graphql/directives.mdfederation.mdinterfaces.mdmigration.mdplugins.mdquick-start.mdresolvers.md详细介绍
适用场景
当用户想要执行以下操作时,请使用此技能:
- 构建或重构 NestJS 应用程序
- 实现控制器、提供者和模块
- 应用中间件、管道、守卫或拦截器
- 配置 DI 作用域、动态模块或生命周期钩子
- 添加验证、序列化、缓存、日志记录或队列
- 集成 GraphQL、WebSockets 或微服务
- 使用 OpenAPI/Swagger 编写 API 文档
- 实现身份验证/授权或加强安全性
- 为 NestJS 编写单元测试或端到端测试
如何使用此技能
-
确定主题,根据用户的请求,在下方的映射中找到对应的示例文件
-
从
examples/目录中加载相应的示例文件 -
遵循该示例文件中有关语法、结构和最佳实践的具体说明
重要说明:
- 所有示例均遵循 NestJS 官方文档结构
- 在适用的情况下,示例同时包含 JavaScript 和 TypeScript 版本
- 每个示例文件都包含关键概念、代码示例和官方文档链接
- 请务必查看示例文件,了解最佳实践和常见模式
-
使用
templates/目录中的模板,加快常见脚手架搭建任务
文档映射(与 https://docs.nestjs.com/ 一一对应)
概览
examples/overview/introduction.md→ https://docs.nestjs.com/examples/fundamentals/first-steps.md→ https://docs.nestjs.com/first-stepsexamples/fundamentals/controllers.md→ https://docs.nestjs.com/controllersexamples/fundamentals/providers.md→ https://docs.nestjs.com/providersexamples/fundamentals/modules.md→ https://docs.nestjs.com/modulesexamples/fundamentals/middleware.md→ https://docs.nestjs.com/middlewareexamples/fundamentals/exception-filters.md→ https://docs.nestjs.com/exception-filtersexamples/fundamentals/pipes.md→ https://docs.nestjs.com/pipesexamples/fundamentals/guards.md→ https://docs.nestjs.com/guardsexamples/fundamentals/interceptors.md→ https://docs.nestjs.com/interceptorsexamples/overview/custom-decorators.md→ https://docs.nestjs.com/custom-decorators
基础
examples/fundamentals/custom-providers.md→ https://docs.nestjs.com/fundamentals/custom-providersexamples/fundamentals/async-providers.md→ https://docs.nestjs.com/fundamentals/async-providersexamples/fundamentals/dynamic-modules.md→ https://docs.nestjs.com/fundamentals/dynamic-modulesexamples/fundamentals/module-ref.md→ https://docs.nestjs.com/fundamentals/module-refexamples/fundamentals/execution-context.md→ https://docs.nestjs.com/fundamentals/execution-contextexamples/fundamentals/lifecycle-events.md→ https://docs.nestjs.com/fundamentals/lifecycle-eventsexamples/fundamentals/injection-scopes.md→ https://docs.nestjs.com/fundamentals/injection-scopesexamples/fundamentals/request-scoped.md→ https://docs.nestjs.com/fundamentals/request-scopedexamples/fundamentals/circular-dependency.md→ https://docs.nestjs.com/fundamentals/circular-dependency Techniques (技巧)examples/techniques/configuration.md→ https://docs.nestjs.com/techniques/configurationexamples/techniques/validation.md→ https://docs.nestjs.com/techniques/validationexamples/techniques/serialization.md→ https://docs.nestjs.com/techniques/serializationexamples/techniques/caching.md→ https://docs.nestjs.com/techniques/cachingexamples/techniques/logger.md→ https://docs.nestjs.com/techniques/loggerexamples/techniques/events.md→ https://docs.nestjs.com/techniques/eventsexamples/techniques/task-scheduling.md→ https://docs.nestjs.com/techniques/task-schedulingexamples/techniques/queues.md→ https://docs.nestjs.com/techniques/queuesexamples/techniques/file-upload.md→ https://docs.nestjs.com/techniques/file-uploadexamples/techniques/streaming-files.md→ https://docs.nestjs.com/techniques/streaming-filesexamples/techniques/database.md→ https://docs.nestjs.com/techniques/databaseexamples/techniques/mongodb.md→ https://docs.nestjs.com/techniques/mongodbexamples/techniques/mongoose.md→ https://docs.nestjs.com/techniques/mongooseexamples/techniques/sequelize.md→ https://docs.nestjs.com/techniques/sequelizeexamples/techniques/prisma.md→ https://docs.nestjs.com/recipes/prisma
Security (安全)
examples/security/authentication.md→ https://docs.nestjs.com/security/authenticationexamples/security/authorization.md→ https://docs.nestjs.com/security/authorizationexamples/security/helmet.md→ https://docs.nestjs.com/security/helmetexamples/security/cors.md→ https://docs.nestjs.com/security/corsexamples/security/rate-limiting.md→ https://docs.nestjs.com/security/rate-limiting
GraphQL
examples/graphql/quick-start.md→ https://docs.nestjs.com/graphql/quick-startexamples/graphql/resolvers.md→ https://docs.nestjs.com/graphql/resolversexamples/graphql/scalars.md→ https://docs.nestjs.com/graphql/scalarsexamples/graphql/interfaces.md→ https://docs.nestjs.com/graphql/interfacesexamples/graphql/unions.md→ https://docs.nestjs.com/graphql/unionsexamples/graphql/directives.md→ https://docs.nestjs.com/graphql/directivesexamples/graphql/plugins.md→ https://docs.nestjs.com/graphql/pluginsexamples/graphql/subscriptions.md→ https://docs.nestjs.com/graphql/subscriptionsexamples/graphql/federation.md→ https://docs.nestjs.com/graphql/federationexamples/graphql/migration.md→ https://docs.nestjs.com/graphql/migration
WebSockets
examples/websockets/gateways.md→ https://docs.nestjs.com/websockets/gatewaysexamples/websockets/exception-filters.md→ https://docs.nestjs.com/websockets/exception-filtersexamples/websockets/guards.md→ https://docs.nestjs.com/websockets/guardsexamples/websockets/interceptors.md→ https://docs.nestjs.com/websockets/interceptorsexamples/websockets/adapters.md→ https://docs.nestjs.com/websockets/adapters
Microservices
examples/microservices/basics.md→ https://docs.nestjs.com/microservices/basicsexamples/microservices/redis.md→ https://docs.nestjs.com/microservices/redisexamples/microservices/mqtt.md→ https://docs.nestjs.com/microservices/mqttexamples/microservices/nats.md→ https://docs.nestjs.com/microservices/natsexamples/microservices/kafka.md→ https://docs.nestjs.com/microservices/kafkaexamples/microservices/grpc.md→ https://docs.nestjs.com/microservices/grpcexamples/microservices/rabbitmq.md→ https://docs.nestjs.com/microservices/rabbitmqexamples/microservices/custom-transport.md→ https://docs.nestjs.com/microservices/custom-transportexamples/microservices/hybrid-application.md→ https://docs.nestjs.com/microservices/hybrid-application
OpenAPI (Swagger)
examples/openapi/introduction.md→ https://docs.nestjs.com/openapi/introductionexamples/openapi/operations.md→ https://docs.nestjs.com/openapi/operationsexamples/openapi/types-and-parameters.md→ https://docs.nestjs.com/openapi/types-and-parametersexamples/openapi/security.md→ https://docs.nestjs.com/openapi/securityexamples/openapi/mapped-types.md→ https://docs.nestjs.com/openapi/mapped-types
CLI、测试与配方
examples/cli/overview.md→ https://docs.nestjs.com/cli/overviewexamples/testing/unit-testing.md→ https://docs.nestjs.com/fundamentals/testingexamples/testing/e2e-testing.md→ https://docs.nestjs.com/fundamentals/testingexamples/recipes/cqrs.md→ https://docs.nestjs.com/recipes/cqrsexamples/recipes/mikroorm.md→ https://docs.nestjs.com/recipes/mikroormexamples/recipes/terminus.md→ https://docs.nestjs.com/recipes/terminusexamples/faq.md→ https://docs.nestjs.com/faq
示例与模板
此技能包含详细示例,按照 NestJS 官方文档结构(https://docs.nestjs.com/)组织。所有示例都位于 examples/ 目录中,并按主题分类(见上方映射)。
使用示例:
- 根据用户的请求确定主题
- 从上方映射中加载相应的示例文件
- 遵循该文件中的说明、语法和最佳实践
- 根据具体用例调整代码示例
使用模板:
- 参考
templates/目录中的模板,进行常见的脚手架搭建 - 模板包括:控制器、提供者、模块、中间件、守卫、管道、拦截器、异常筛选器、DTO 和项目结构
- 根据具体需求和编码风格调整模板
最佳实践
- 使用依赖注入:利用 NestJS DI 容器,提高可测试性和可维护性
- 按模块组织:将相关功能归入功能模块
- 一致地使用装饰器:遵循 NestJS 在控制器、提供者和路由中的装饰器模式
- 验证输入数据:使用 ValidationPipe 和 DTO 验证请求
- 妥善处理错误:实现全局异常筛选器,确保错误处理一致
- 使用守卫进行授权:使用守卫保护路由,而不是内联检查
- 充分利用拦截器:使用拦截器处理日志记录和转换等横切关注点
- 类型安全:使用 TypeScript 提高类型安全性和开发体验
- 全面测试:为服务编写单元测试,为 API 端点编写 E2E 测试
- 遵循官方模式:参考官方文档,了解推荐的模式和实践
资源
- 官方文档:https://docs.nestjs.com/
- GitHub 仓库:https://github.com/nestjs/nest
关键词
NestJS、Node.js、框架、控制器、提供者、模块、中间件、异常筛选器、管道、守卫、拦截器、依赖注入、GraphQL、WebSocket、微服务、OpenAPI、Swagger、测试、验证、缓存、日志记录、TypeScript、装饰器、DI、IoC、控制器、提供者、模块、中间件、守卫、管道、拦截器、依赖注入
评论
支持 Markdown