背景
バックエンドの開発時に別の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...