Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8697

typescriptでバックエンド開発 Nest.jsのチュートリアルをやってみた

$
0
0
はじめに typescriptでバックエンドを作りたいと思ったとき、以前はExpressをtypeScriptで実装してGulpでビルドする方法しかないものだっと思っていました。(それかdeno?)最近になってnestjsというフレームワークにたどり着いたので記載します。 cliのインストール npm i -g @nestjs/cli プロジェクトの作成 nestSampleというプロジェクトを作成 nest new nestSample パッケージマネージャをnpmかyarnを選択できるようです。(ここではnpmを選択 CREATE nest-sample/.eslintrc.js (631 bytes) CREATE nest-sample/.prettierrc (51 bytes) CREATE nest-sample/README.md (3339 bytes) CREATE nest-sample/nest-cli.json (64 bytes) CREATE nest-sample/package.json (1973 bytes) CREATE nest-sample/tsconfig.build.json (97 bytes) CREATE nest-sample/tsconfig.json (339 bytes) CREATE nest-sample/src/app.controller.spec.ts (617 bytes) CREATE nest-sample/src/app.controller.ts (274 bytes) CREATE nest-sample/src/app.module.ts (249 bytes) CREATE nest-sample/src/app.service.ts (142 bytes) CREATE nest-sample/src/main.ts (208 bytes) CREATE nest-sample/test/app.e2e-spec.ts (630 bytes) CREATE nest-sample/test/jest-e2e.json (183 bytes) ? Which package manager would you ❤️ to use? (Use arrow keys) ❯ npm yarn インストールが完了したようです。 ✔ Installation in progress... ☕ 🚀 Successfully created project nest-sample 👉 Get started with the following commands: $ cd nest-sample $ npm run start Thanks for installing Nest 🙏 Please consider donating to our open collective to help us maintain this package. 🍷 Donate: https://opencollective.com/nest ディレクトリの構成は以下のような感じでした。 tree -I node_modules . ├── nest-cli.json ├── package.json ├── package-lock.json ├── README.md ├── src │   ├── app.controller.spec.ts │   ├── app.controller.ts │   ├── app.module.ts │   ├── app.service.ts │   └── main.ts ├── test │   ├── app.e2e-spec.ts │   └── jest-e2e.json ├── tsconfig.build.json └── tsconfig.json .gitがあったので gitのリポジトリが勝手に出来上がっているようです。 出来上がったコードを確認 エンドポイント main.ts import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); } bootstrap(); 読み込んでいるAppModule app.module.ts import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ imports: [], controllers: [AppController], providers: [AppService], }) export class AppModule {} 更に読み込んでいる AppController,AppServiceを確認 app.controller.ts import { Controller, Get } from '@nestjs/common'; import { AppService } from './app.service'; @Controller() export class AppController { constructor(private readonly appService: AppService) {} @Get() getHello(): string { return this.appService.getHello(); } } app.service.ts import { Injectable } from '@nestjs/common'; @Injectable() export class AppService { getHello(): string { return 'Hello World!'; } } controllerのクラスにserviceをコンストラクタで注入しているのがわかります。(DIパターンというやつですかね。 とりあえず動かしてみます。 npm start > nest-sample@0.0.1 start > nest start [Nest] 3843 - 07/02/2021, 11:59:43 PM [NestFactory] Starting Nest application... [Nest] 3843 - 07/02/2021, 11:59:43 PM [InstanceLoader] AppModule dependencies initialized +133ms [Nest] 3843 - 07/02/2021, 11:59:43 PM [RoutesResolver] AppController {}: +26ms [Nest] 3843 - 07/02/2021, 11:59:43 PM [RouterExplorer] Mapped {, GET} route +12ms [Nest] 3843 - 07/02/2021, 11:59:43 PM [NestApplication] Nest application successfully started +10ms とりあえず動きました。 まだまだいろいろな機能がありそうなので少しづつ掘り出していければと思います。 typeORMの使用が推奨されているようなので、typeORMを使用した単純なCRUDでも作ってみれば色々とわかってくるかもしれません。

Viewing all articles
Browse latest Browse all 8697

Trending Articles