3
1

More than 1 year has passed since last update.

30秒でREST APIのモックサーバを準備する

Posted at

背景

バックエンドの開発時に別のAPIサーバへ問い合わせするパターンがあるじゃないですか。
個人開発なら「httpbin.org」とか使えばいいのですが、業務だと外部サーバーに投げるわけにいきません。
というわけで30秒でサクッと作れるREST APIサーバである「json-server」の紹介です。

1. インストール

※レギュレーションとしてパッケージのインストール時間は除外します!

$ npm i json-server

2. データを準備(15秒)

パスと応答データを記載したjsonファイルを準備する。

dummy.json
{
  "top_gear": [
    {
      "id": 0,
      "name": "Jeremy Charles Robert Clarkson",
      "keyword ": "英国紳士(笑)"
    },
    {
      "id": 1,
      "name": "Richard Mark Hammond",
      "keyword ": "事故"
    },
    {
      "id": 2,
      "name": "James Daniel May",
      "keyword ": "老犬"
    }
  ]
}

3. サーバ起動(5秒)

$ json-server --watch dummy.json

以下のログが出れば起動完了です。

  \{^_^}/ hi!

  Loading dummy.json
  Done

  Resources
  http://localhost:3000/top_gear

  Home
  http://localhost:3000

  Type s + enter at any time to create a snapshot of the database
  Watching...

http://localhost:3000/top_gearにアクセスするとJsonが返ってきます。
image.png

3
1
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
3
1