プロジェクトをインストールして作成
一時的なDockerコンテナーを作成する
Make temporary docker container
sudo docker container run -it -v $(pwd):/app -w /app node:12.13.1-alpine /bin/sh
そのDockerコンテナ内に「nest-cli」をインストールして新しいプロジェクトを作成します
Inside that docker container, install the nest-cli and make a new project
$ npm i -g @nestjs/cli
$ nest new project-name
プロジェクトが作成されたら、プロジェクトの内部に入り、依存関係をインストールします
Once your project is created, get inside the project, and install dependencies
$ cd project
$ npm install
$ npm run start
完了したら、コンテナを終了できます。
Once done, you can exit the container.
プロジェクトファイルの所有者を変更します。
Change the owner od the project files.
sudo chown -R myuser:myuser <project-name>
開発モードを実行するようにDockerを構成する
プロジェクトディレクトリ内に「Dockerfile」を作成します
Inside the project directory, create a Dockerfile
FROM node:12.13.1-alpine
WORKDIR /app
RUN apk update && \
apk add git && \
npm install -g npm && \
npm install -g @nestjs/cli
ENV HOST 0.0.0.0
EXPOSE 3000
また、 docker-compose.yamlも作成します
and also create a docker-compose.yaml
version: '3'
services:
nest:
build: .
tty: true
command: npm run start:dev
volumes:
- .:/app
ports:
- "3000:3000"
作成後、 localhost:3000にアクセスすると、 Hello Worldページが表示されます。
Upon creation, you can visit localhost:3000, you should see the Hello World page.
ウォッチをテストするには、 app.services.tsのgetHello()を編集します。編集したら、ページを更新して変更を確認できます。
To test the watch, you can edit the app.services.ts getHello(), once edited, you can refresh the page, to see the change.
新しいコントローラーを作成する
sudo docker-compose exec nest nest generate controller cats
これはプロジェクトの srcディレクトリの中に catsディレクトリを作成します
This will create a cats directory inside the src directory of the project
私の例をコピーして貼り付けることができます
You can copy-paste my example
import { Controller, Get, Req, Param } from '@nestjs/common';
@Controller('cats')
export class CatsController {
@Get()
findAll():Object{
var test = {id:1,name:"my cat"}
return test;
}
@Get("filter")
getFilter(@Req() req):Object{
return {id:req.query.id,name:"my cat id"};
}
@Get(':id')
findOne(@Param() params): string {
return `This action returns a #${params.id} cat`;
}
}
localhost:3000/
localhost:3000/filter?id=55
localhost:3000/20