5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Nest.js で簡単な WebAPI を作成

Last updated at Posted at 2021-02-10

ほぼ、こちらのページと同様です。
NestJS CLIで初心者でも簡単にNode.js REST APIが作れる!

バージョンと、題材を日本文学にしたことが異なります。

環境
Arch Linux で動かしました。

$ nest info

 _   _             _      ___  _____  _____  _     _____
| \ | |           | |    |_  |/  ___|/  __ \| |   |_   _|
|  \| |  ___  ___ | |_     | |\ `--. | /  \/| |     | |
| . ` | / _ \/ __|| __|    | | `--. \| |    | |     | |
| |\  ||  __/\__ \| |_ /\__/ //\__/ /| \__/\| |_____| |_
\_| \_/ \___||___/ \__|\____/ \____/  \____/\_____/\___/


[System Information]
OS Version     : Linux 5.15
NodeJS Version : v17.3.0
NPM Version    : 8.3.0 

[Nest CLI]
Nest CLI Version : 7.6.0 

[Nest Platform Information]
platform-express version : 7.5.1
common version           : 7.5.1
core version             : 7.5.1

プロジェクトの作成

nest new -p npm project02

動作の確認

cd project02
npm run start:dev

クライアントでアクセス

$ curl http://localhost:3000
Hello World!

Module の作成

nest generate module test

Interface の作成

nest generate interface test/test

src/test/test.interface.ts の修正

src/test/test.interface.ts
export interface Test {
	id: string;
	name: string;
	author: string;
}

Service の作成

nest generate service test

src/test/test.service.ts の修正

src/test/test.service.ts
import { Injectable } from '@nestjs/common';
import { Test } from './test.interface';

@Injectable()
export class TestService {
private readonly test: Test[] = [
    {id: 't101', name: '草枕', author: '夏目漱石' },
    {id: 't102',  name: '走れメロス', author: '太宰治' },
    {id: 't103', name: '千曲川のスケッチ', author: '島崎藤村' },
    {id: 't104', name: '高瀬舟', author: '森鴎外' }
]

// private readonly test: Test[] = [{id: 'a', name: 'Alex'}, {id: 'b', name: 'Bob'}, {id: 'c', name: 'Cathy'}];

  listTest(): Test[] {
    return this.test;
  }

  getTest(id: string): Test {
    return this.test.find(value => value.id === id);
  }
}

Controller の作成

nest generate controller test

src/test/test.controller.ts の修正

src/test/test.controller.ts
import { Controller,Get, Param } from '@nestjs/common';
import { TestService } from './test.service';
import { Test } from './test.interface';

@Controller('test')
export class TestController {
    constructor(private readonly testService: TestService) {}

    @Get()
    listUsers(): Test[] {
      return this.testService.listTest();
    }

    @Get(':id')
    getTest(@Param('id') id: string): Test {
      return this.testService.getTest(id);
    }      
}

サーバーの起動

nest start

クライアントでアクセス

全件取得

$ curl http://localhost:3000/test | jq .
[
  {
    "id": "t101",
    "name": "草枕",
    "author": "夏目漱石"
  },
  {
    "id": "t102",
    "name": "走れメロス",
    "author": "太宰治"
  },
  {
    "id": "t103",
    "name": "千曲川のスケッチ",
    "author": "島崎藤村"
  },
  {
    "id": "t104",
    "name": "高瀬舟",
    "author": "森鴎外"
  }
]

id を指定して取得

$ curl http://localhost:3000/test/t102 | jq .
{
  "id": "t102",
  "name": "走れメロス",
  "author": "太宰治"
}
5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?