LoginSignup
1
1

More than 5 years have passed since last update.

json-serverを使ったAPIモック作成メモ

Last updated at Posted at 2018-12-06

フロントでAPIの実装したくてもできない時にローカル環境にAPIモックを作れるサービスを探してみたらありました。
↓公式リポジトリこちらです。
json-server

インストール

インストール方法はこちらです。

$ npm install -g json-server

使い方

1. db.jsonというファイルを作成

db.json
{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ],
  "profile": { "name": "typicode" }
}

2. json-server起動

$ json-server --watch db.json

3. アクセスする

起動させるとこんな感じでターミナルに表示されます。
スクリーンショット 2018-12-06 9.39.55.png
該当するURLにアクセスできます。

別タブでターミナル開いてcurl

curlで確認する場合はこう。

$ curl -i http://localhost:3000/comments  

POSTの時

$ curl -i -X POST http://localhost:3000/comments -d body=test

あとは実際の使いたいAPIに合わせてjsonを変更

あとは実際の使いたいAPIに合わせてdb.jsonを変更していけば良い。

参考

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