リリース・改善中
NestJS · Express ガイド · 2/6
この章は現在、英語でのみ提供しています。
Express does not prescribe a folder layout, while NestJS has clear conventions for organizing code into modules. This chapter walks through the files nest new generates, the key configuration, feature modules, and a common way to organize an Express project.
A project created with nest new looks roughly like this (ESLint and Prettier config omitted).
hello-nest/
├── src/
│ ├── app.controller.spec.ts
│ ├── app.controller.ts
│ ├── app.module.ts
│ ├── app.service.ts
│ └── main.ts
├── test/
│ ├── app.e2e-spec.ts
│ └── jest-e2e.json
├── nest-cli.json
├── package.json
├── tsconfig.build.json
└── tsconfig.json| File | Role |
|---|---|
src/main.ts | Entry point: creates the app with NestFactory, applies global setup, listens on a port |
src/app.module.ts | Root module that imports every other module |
src/app.controller.ts | Sample controller handling one route |
src/app.service.ts | Sample service (provider) injected into the controller |
src/*.spec.ts | Unit tests kept next to the source |
test/ | End-to-end tests that boot the whole app and talk HTTP |
nest-cli.json controls the Nest CLI. sourceRoot points to the source folder, and deleteOutDir clears dist before each build.
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}In tsconfig.json, two options are essential for Nest: experimentalDecorators enables decorator syntax such as @Controller(), and emitDecoratorMetadata keeps constructor parameter types at runtime so dependency injection knows which class to provide. tsconfig.build.json excludes test files from the build.
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"outDir": "./dist",
"baseUrl": "./"
}
}This shows only the Nest-related options. The generated file also sets module, target, strictNullChecks and more, and defaults vary slightly between Nest releases.
As an application grows, each feature gets its own module. nest g resource creates the controller, service, module, DTOs and entity in one go, and for a REST API it also scaffolds CRUD endpoints.
nest g resource userssrc/users/
├── dto/
│ ├── create-user.dto.ts
│ └── update-user.dto.ts
├── entities/
│ └── user.entity.ts
├── users.controller.spec.ts
├── users.controller.ts
├── users.module.ts
├── users.service.spec.ts
└── users.service.tsBy convention, file names follow <name>.<type>.ts in kebab case, and classes use PascalCase such as UsersController and UsersService. A module declares its own controllers and providers and lists in exports only the providers other modules need.
// src/users/users.module.ts
import { Module } from "@nestjs/common";
import { UsersController } from "./users.controller";
import { UsersService } from "./users.service";
@Module({
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}The CLI adds UsersModule to the root module's imports automatically. Shared guards, interceptors and utilities often live in a folder such as src/common/.
Because Express enforces no structure, teams decide for themselves. A common approach splits routers, middleware and business logic into folders, and keeps creating the app separate from listening on a port.
my-express-api/
├── src/
│ ├── routes/
│ │ └── users.js
│ ├── middleware/
│ │ └── error-handler.js
│ ├── services/
│ │ └── users-service.js
│ ├── app.js
│ └── server.js
└── package.jsonapp.js only builds and exports the app; server.js opens the port. Tests can then import app without binding to a port.
// src/app.js
const express = require("express");
const usersRouter = require("./routes/users");
const app = express();
app.use(express.json());
app.use("/users", usersRouter);
module.exports = app;// src/server.js
const app = require("./app");
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on ${port}`));src/main.ts (entry point) and src/app.module.ts (root module).experimentalDecorators and emitDecoratorMetadata are required for decorators and dependency injection.nest g resource generates the controller, service and DTOs together.app from listen to make testing easy.
コメント 0件
ログイン · ログインするとコメントできます。
最初のコメントを書いてみましょう。