项目结构与 CLI
学习使用 CLI 初始化新的 NestJS 项目,理解默认文件夹结构,并高效生成组件。
项目结构与 CLI 是 CoddyKit 上的免费 NestJS Enterprise Backend APIs 课时。 这是第 2 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 NestJS Enterprise Backend APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 NestJS Enterprise Backend APIs 课程共包含 3 节课。
本课时的部分内容尚未翻译,以英文显示。
NestJS CLI: Your Development Ally
The NestJS CLI is your dev assistant — it scaffolds projects, generates boilerplate for modules and services, and runs your builds.
Get the CLI Up and Running
Install the CLI globally with npm i -g @nestjs/cli (Node and npm required first). The -g flag makes the nest command available everywhere.
Starting a New NestJS App
Create a project with nest new my-first-backend. The CLI asks for a package manager, then sets up files, dependencies, and boilerplate.
Understanding the Project Layout
A new project's layout: src/ holds your code, node_modules/ the dependencies, test/ e2e tests, and dist/ the compiled output.
Core Application Files in `src`
Inside src/ are the core files: main.ts bootstraps the app, app.module.ts is the root module, plus a starter controller and service.
`main.ts`: Application Entry Point
main.ts is the entry point: NestFactory.create() builds the app and app.listen() starts the server, usually on port 3000.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();Building Blocks: A Simple Class
NestJS runs on classes — controllers, services, and modules are all TypeScript classes. This runnable snippet shows the basic class structure.
class Greeter {
name: string;
constructor(name: string) {
this.name = name;
}
greet(): string {
return `Hello, ${this.name}! Welcome to CoddyKit.`;
}
}
// This is the entry point for running the example
function main() {
const greeter = new Greeter("NestJS Learner");
console.log(greeter.greet());
}
main();Streamline with `nest generate`
As the app grows, use nest generate (or nest g) to scaffold modules, controllers, services, guards, pipes — and auto-wire them in.
CLI in Action: New Module
For a Users feature, run nest g module users. The CLI creates the folder and users.module.ts, then registers it in your root AppModule.
Test Your CLI Knowledge
You've learned about the NestJS CLI and how it helps set up and structure your project.
Which CLI command is used to quickly create a new NestJS project named my-api?
Summary: CLI & Project Setup
You used the CLI: installed it, created a project with nest new, explored the layout, and scaffolded components with nest generate.
常见问题解答
「项目结构与 CLI」课时是免费的吗?
是的 — 「项目结构与 CLI」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 NestJS Enterprise Backend APIs 课程的其余内容,请升级到 CoddyKit PRO。 NestJS Enterprise Backend APIs 课程共包含 3 节课。
「项目结构与 CLI」这节课中我会学到什么?
学习使用 CLI 初始化新的 NestJS 项目,理解默认文件夹结构,并高效生成组件。 你通过在浏览器中直接运行的动手代码来练习 NestJS Enterprise Backend APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 NestJS Enterprise Backend APIs 需要有经验吗?
无需任何先前经验。CoddyKit 上的 NestJS Enterprise Backend APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 3 节。
「项目结构与 CLI」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 NestJS Enterprise Backend APIs 课中编写并运行代码吗?
能。每节 NestJS Enterprise Backend APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- NestJS 框架简介
- 项目结构与 CLI
- 模块、控制器与服务