1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】モックサーバー(Mock Server)を3分で構築する

Posted at

はじめに

モックサーバーとは、フロントエンド開発やテスト時に、バックエンドのAPIができていない状態でもダミーのレスポンスを返すことができる模擬サーバーです。

実際にやってみた

どのディレクトリでも良いので下記コマンドを実行します

npm install -g json-server

パスを通します

zsh
echo 'export PATH=$HOME/.npm-global/bin:$PATH' >> ~/.zshrc
source ~/.zshrc

ディレクトリを作成し,その中にJSONファイルを作成します。
ファイル名はdb.jsonです

mkdir test && cd test && vim db.json
db.jsonサンプル
{
  "users": [
    { "id": 1, "name": "Alice" },
    { "id": 2, "name": "Bob" }
  ]
}

動作確認

ポート3000でアクセスします。
必要に応じて空いているポート番号に変更してください

json-server --watch db.json --port 3000

image.png

db.jsonのidやnameを変更してブラウザをリロードすると画面表示も変わります

db.json
 "users": [
    { "id": 1, "name": "Alice" },
    { "id": 2, "name": "Bob" },
    { "id": 3, "name": "pon" }

image.png

ctr + C で終了できます。

まとめ

簡単にモックサーバーを作成してみました。開発で使っていこうと思います。

参考文献

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?