LoginSignup
3

More than 5 years have passed since last update.

json-server使ってみた

Last updated at Posted at 2016-01-10

参考

npmでインストールする場合

sudo apt-get install -y npm
npm install json-server
node_modules/.bin/json-server db.json -p 8080

dockerで動かす場合

256mb
docker pull clue/json-server
db.json
{
  "users": [
    { 
      "id": 1,
      "name": "user1"
    }
  ]
}
起動
docker run -d \
 -p 8080:80 \
 -v `pwd`:/data/ \
 clue/json-server

curlで操作

~/.bashrc
json() {
  URL=http://localhost:8080

  case ${1} in
    add|a|i|insert) ACT="POST" ;;
    update|u) ACT="PUT" ;;
    *) ACT="GET" ;;
  esac

  ARG2=${2}

  if [ $# -ge 2 ];then
    shift;shift;set -x
    curl -X ${ACT} ${URL}${ARG2} $@
  else
    echo "引数が足りません."$#
  fi
}
#一覧
json list /users

#追加
json a /users/2 -d name=user2

#更新
json u /users/2 -d name=おなまえ

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