LoginSignup
17
19

More than 5 years have passed since last update.

stubcellを使ってみた

Posted at

紹介

リポジトリはこちら https://github.com/yosuke-furukawa/stubcell

要するに、json返すサーバーのモックが簡単に作れる的なもの
動くドキュメント的な用途で、ローカルで動す or 開発最初期にとりあえず実際のサーバーに立てておいても良いのではないかという感じですね

バージョンがまだ1.0になっていないのでこれから感はありますが、期待をこめてメモ残しておきます

インストール

$ npm install stubcell

簡単!

プロジェクト作成

まずは適当にディレクトリ作成

$ mkdir mock-server
$ cd mock-server

entry.yaml

何はなくともこれが必要

今回はこんなかんじでGETのみ用意
レスポンスには bodyで直書き or fileで切り出し

- # ルートのアクセステスト
  request:
    url: /
    method: GET
  response:
    status: 200
    body: '{ result: "ok" }'

- # ユーザーリスト
  request:
    url: /users
    method: GET
  response:
    status: 200
    file: users/index.json

- # ユーザー指定
  request:
    url: /users/:id
    method: GET
  response:
    status: 200
    file: users/show.json

json file

必要なjsonファイルを作成

$ mkdir users
$ touch users/index.json
$ touch users/show.json

中身はこんな感じ

users/index.json
{
    // stubcellのメリットとしてコメントが使えるらしいです( JSON5
    "users": [
        {
            "id": 1,
            "name": "user1",
            "message": "hello i'm 1"
        },
        {
            "id": 2,
            "name": "user2",
            "message": "hello i'm 2"
        },
        {
            "id": 3,
            "name": "user3",
            "message": "hello i'm 3"
        },
        {
            "id": 4,
            "name": "user4",
            "message": "hello i'm 4"
        },
        {
            "id": 5,
            "name": "user5",
            "message": "hello i'm 5"
        }
    ]
}
show.json
{
    "id": 1,
    "name": "user1",
    "message": "hello i'm 1"
}

サーバー起動

$ npm install stubcell -g
$ stubcell

で、とりあえず起動できます
デフォルトならlocalhost:8090に立つので、アクセスを確認して見てください

オプションなどは https://github.com/yosuke-furukawa/stubcell へー

17
19
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
17
19