0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Nest.js その2 appModule.ts

Posted at

appModule.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserModule } from './users/users.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppDataSource } from './config/typeorm.config';
import { TodoModule } from './todos/todos.module';
import { NewsModule } from './news/news.module';

@Module({
  imports: [
    UserModule,
    TodoModule,
    NewsModule,
    TypeOrmModule.forRoot(AppDataSource.options),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}




どの機能を組み込むかをまとめている場所

@Module({
  imports: [
    UserModule,
    TodoModule,
    NewsModule,
    TypeOrmModule.forRoot(AppDataSource.options),
  ],




AppDataSource.options
.options を渡すことで、forRoot() が正しく DB(データベース) に接続してくれる。

forRoot(AppDataSource.options),





アプリで使う「コントローラー(Controller)」を指定する。
URL に対応した関数をまとめておく場所

 controllers: [AppController],





アプリで使う「サービス(Service)」を指定する
ビジネスロジック(処理の中身)を担当する場所
たとえば、データベースからデータを取ってきたり、計算したり、メールを送ったりする。

providers: [AppService],




NestJS のアプリは「モジュール」を組み合わせて作っていく。
その中でも一番最初に読み込まれる メインモジュール が AppModule。
NestJS アプリの「メインモジュール(ルートモジュール)」を定義してエクスポートしている。
export をつけることで、他のファイル(main.ts)からも使えるようにしている。

export class AppModule {}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?