NestJS をシンプルに初期構築から試す（その１nest cliを使いプロジェクト初期化）

# はじめに
- [NestJS + AWS Lambdaとして動かすまでは のシリーズ](https://qiita.com/ssugimoto/items/74c26a8219a944d4acc4)で実施した内容を exampleに依存せず実施
- もっとシンプルにゼロから始める

### 記載する予定の内容です
- その１ nest cliを使いプロジェクト初期化（本ページ）
- [その２ AWS Lambda + API Gatewayとしてデプロイする](https://qiita.com/ssugimoto/items/f13b3a9d871734a912bb)
- [その３ AWS App Runner として動かす（仮）](https://qiita.com/ssugimoto/items/3db0616186ac545339e5/)

## GitHubリポジトリ
- 試した内容は、GitHubに公開しています
- https://github.com/ssugimoto/serverless-framework-nestjs-example/branches
 
|記事 |ブランチ |内容|
|---|---|---|
|[その１](https://github.com/ssugimoto/serverless-framework-nestjs-example/tree/feature/example-step1)|feature/example-step1 |nest cli |
|[その２](https://github.com/ssugimoto/serverless-framework-nestjs-example/tree/feature/example-step2-sls)|feature/example-step2-sls |use serverless framework, sls deploy|

将来、１つのdevelop ブランチ にしてしまうかもしれません


## 最低限の準備
- Node.js 18 が動く環境であること（v18.16.0を利用）
- NestJS v10
- Serverless Framework v3
- なお、npm では ライブラリを Globalインストールをするため、コンテナ等の環境が好ましい

#### Node バージョン

```
$ node -v
v18.16.0
```

#### NestJS CLIをインストール

```
npm i -g @nestjs/cli
```

```
$ npm i -g @nestjs/cli

added 263 packages in 14s

44 packages are looking for funding
  run `npm fund` for details
```

```
$ nest -v
10.1.12
```

#### Serverless Frameworkをインストール

```
npm i -g serverless
```

```
$ npm i -g serverless
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated querystring@0.2.1: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated superagent@7.1.6: Please downgrade to v7.1.5 if you need IE/ActiveXObject support OR upgrade to v8.0.0 as we no longer support IE and published an incorrect patch version (see https://github.com/visionmedia/superagent/issues/1731)

added 418 packages in 19s

68 packages are looking for funding
  run `npm fund` for details
node ➜ /workspaces/nestjs-example $ 
````


```
node ➜ /workspaces/nestjs-example $ sls -v
Framework Core: 3.34.0
Plugin: 6.2.3
SDK: 4.3.2
```


## Nestプロジェクト初期化

nest cli を使い serverless-nestjs でのプロジェクトを作成

https://docs.nestjs.com/cli/usages
https://docs.nestjs.com/faq/serverless


nest new プロジェクト名 で作成
```
$ nest new serverless-nest
```

#### コマンド実行結果
```
$ nest new serverless-nest
⚡  We will scaffold your app in a few seconds..

? Which package manager would you ❤️  to use? npm
CREATE serverless-nest/.eslintrc.js (663 bytes)
CREATE serverless-nest/.prettierrc (51 bytes)
CREATE serverless-nest/README.md (3340 bytes)
CREATE serverless-nest/nest-cli.json (171 bytes)
CREATE serverless-nest/package.json (1956 bytes)
CREATE serverless-nest/tsconfig.build.json (97 bytes)
CREATE serverless-nest/tsconfig.json (546 bytes)
CREATE serverless-nest/src/app.controller.spec.ts (617 bytes)
CREATE serverless-nest/src/app.controller.ts (274 bytes)
CREATE serverless-nest/src/app.module.ts (249 bytes)
CREATE serverless-nest/src/app.service.ts (142 bytes)
CREATE serverless-nest/src/main.ts (208 bytes)
CREATE serverless-nest/test/app.e2e-spec.ts (630 bytes)
CREATE serverless-nest/test/jest-e2e.json (183 bytes)

✔ Installation in progress... ☕

🚀  Successfully created project serverless-nest
👉  Get started with the following commands:

$ cd serverless-nest
$ 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
                                                                   
```


#### 作成された初期ディレクトリ

- 必要なディレクトリとファイルは作成される

```
node ➜ /workspaces/nestjs-example $ tree -L 2
.
└── serverless-nest
    ├── nest-cli.json
    ├── node_modules
    ├── package.json
    ├── package-lock.json
    ├── README.md
    ├── src
    ├── test
    ├── tsconfig.build.json
    └── tsconfig.json

4 directories, 6 files
```


NestJSアプリケーション実行
```
npm start
```


```
node ➜ /workspaces/nestjs-example/serverless-nest (master) $ npm start

> serverless-nest@0.0.1 start
> nest start

[Nest] 2367  - 08/21/2023, 1:34:13 AM     LOG [NestFactory] Starting Nest application...
[Nest] 2367  - 08/21/2023, 1:34:13 AM     LOG [InstanceLoader] AppModule dependencies initialized +8ms
[Nest] 2367  - 08/21/2023, 1:34:13 AM     LOG [RoutesResolver] AppController {/}: +13ms
[Nest] 2367  - 08/21/2023, 1:34:13 AM     LOG [RouterExplorer] Mapped {/, GET} route +1ms
[Nest] 2367  - 08/21/2023, 1:34:13 AM     LOG [NestApplication] Nest application successfully started +2ms
```

- http://localhost:3000/

- Windows のコマンドプロンプト
```
>curl http://localhost:3000/
Hello World!
```

- WindowsのPower Shell
```
PS > curl http://localhost:3000/

StatusCode        : 200
StatusDescription : OK
Content           : Hello World!
RawContent        : HTTP/1.1 200 OK
                    Connection: keep-alive
                    Keep-Alive: timeout=5
                    Content-Length: 12
                    Content-Type: text/html; charset=utf-8
                    Date: Mon, 21 Aug 2023 01:35:22 GMT
                    ETag: W/"c-Lve95gjOVATpfV8EL5X4nxwjKHE"...
Forms             : {}
Headers           : {[Connection, keep-alive], [Keep-Alive, timeout=5], [Content-Length, 12], [Content-Type, text/html;
                     charset=utf-8]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 12
```

ここまでは、Webフレームワーク としての初期化です
次に、AWS上へアプリケーションのデプロイを試していきます。