Open12
Heroku + NestJS で RESTAPI を作成する
ピン留めされたアイテム

memo 全体の構成
- local で nest 立てる
- heroku にあげる
- local で postgres つなぐ
- heroku にあげる
- jest で unit test する
- supertest で E2E する

nestjs
getting started はない!(有料

まずはグローバル
npm i -g @nestjs/cli
インストール確認は
$ nest -v

雛形作成
$ nest new my-nest-sample

雛型、こんな感じ

package.json はこんな感じ。 開発サーバはnpm run start:debug
で立てる。
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
...
},
localhost:3000 で hello world が表示されればOK

heroku を使う
$ brew tap heroku/brew && brew install heroku
ログインする
$ heroku login
$ heroku whoami
$ heroku apps:create
$ git remote -v
$ git push heroku main
$ heroku open -> エラー

main.ts の修正
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT || 3000);
}
bootstrap();

Procfile の追加
web: npm run start:prod

これで表示される。
$ heroku open

ローカルに postgres を入れる

これ使うと構成がだいぶ綺麗になる。
ログインするとコメントできます