・2019/11/10
Raspberry Piで WebAPIモックサーバー JSON Serverを動かしてみるテスト
(Raspberry Piで APIモックサーバー JSON Serverを動かしてみるテスト)
Tags: [Raspberry Pi], [電子工作], [セキュリティ]
● Raspberry Piで WebAPIモックサーバー JSON Serverを動かしてみるテスト
JSON Server
Get a full fake REST API with zero coding in less than 30 seconds (seriously)
● Getting started - Install JSON Server
npmが必要なので最初に npmをインストールする。
最新版は Node.js v13だが、最新は避けて Node.js v10をインストールする。
pi@raspberrypi:~ $ npm install -g json-server
-bash: npm: command not found
# https://github.com/nodesource/distributions
# Node.js v10.x
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
# ## Run `sudo apt-get install -y nodejs` to install Node.js 10.x and npm
# ## You may also need development tools to build native addons:
# sudo apt-get install gcc g++ make
# ## To install the Yarn package manager, run:
# curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
# echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
# sudo apt-get update && sudo apt-get install yarn
sudo apt-get install -y nodejs
# バージョンを確認する
node -v
# v10.18.0
npm -v
# 6.13.4
pi@raspberrypi:~ $ npm install -g json-server
npm WARN checkPermissions Missing write access to /usr/lib/node_modules
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/lib/node_modules'
npm ERR! { [Error: EACCES: permission denied, access '/usr/lib/node_modules']
npm ERR! stack:
npm ERR! 'Error: EACCES: permission denied, access \'/usr/lib/node_modules\'',
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/usr/lib/node_modules' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/pi/.npm/_logs/2019-12-30T11_34_01_889Z-debug.log
permission deniedが出たので sudoを付けて実行する。(何とかの一つ覚え)
sudo npm install -g json-server
# /usr/bin/json-server -> /usr/lib/node_modules/json-server/lib/cli/bin.js
# + json-server@0.15.1
# added 237 packages from 128 contributors in 125.491s
●サンプルの db.jsonファイルを作成する
{
"posts": [
{ "id": 1, "title": "json-server1", "author": "typicode1" },
{ "id": 2, "title": "json-server2", "author": "typicode2" },
{ "id": 5, "title": "json-server5", "author": "typicode1" },
{ "id": 8, "title": "json-server8", "author": "typicode2" }
],
"comments": [
{ "id": 1, "body": "some comment1-1", "postId": 1 },
{ "id": 2, "body": "some comment2-2", "postId": 2 },
{ "id": 3, "body": "some comment3-1", "postId": 1 },
{ "id": 5, "body": "some comment5-5", "postId": 5 },
{ "id": 7, "body": "some comment7-8", "postId": 8 },
{ "id": 9, "body": "some comment9-1", "postId": 1 }
],
"profile": { "name": "typicode1" }
}
● JSON Serverを開始する
json-server --watch db.json -H <ラズパイの IPアドレス>
注:下記で実行した場合、ラズパイの中で wget http://localhost:3000/posts は成功しますが、外部から http://<ラズパイの IPアドレス>:3000/posts としても ERR_CONNECTION_REFUSEDで接続が拒否されます。
json-server --watch db.json
# -p 8080でポートが 8080になります
pi@raspberrypi:~ $ json-server --watch db.json -p 8080 -H 192.168.123.234
\{^_^}/ hi!
Loading db.json
Done
Resources
http://192.168.123.234:8080/posts
http://192.168.123.234:8080/comments
http://192.168.123.234:8080/profile
Home
http://192.168.123.234:8080
Type s + enter at any time to create a snapshot of the database
Watching...
GET /posts 200 31.039 ms - 77
GET /posts/1 200 17.067 ms - 63
●動作確認
ブラウザで下記の URLにアクセスする。
pi@raspberrypi:~ $ json-server --watch db.json -p 8080 -H 192.168.123.234
http://<ラズパイの IPアドレス>:8080/posts/1
http://192.168.123.234:8080/posts/1
ブラウザの画面に下記が表示されれば成功。
http://192.168.123.234:8080/posts/1
{
"id": 1,
"title": "json-server1",
"author": "typicode1"
}
http://192.168.123.234:8080/posts
[
{
"id": 1,
"title": "json-server1",
"author": "typicode1"
},
{
"id": 2,
"title": "json-server2",
"author": "typicode2"
},
{
"id": 5,
"title": "json-server5",
"author": "typicode1"
},
{
"id": 8,
"title": "json-server8",
"author": "typicode2"
}
]
http://192.168.123.234:8080/comments
[
{
"id": 1,
"body": "some comment1-1",
"postId": 1
},
{
"id": 2,
"body": "some comment2-2",
"postId": 2
},
{
"id": 3,
"body": "some comment3-1",
"postId": 1
},
{
"id": 5,
"body": "some comment5-5",
"postId": 5
},
{
"id": 7,
"body": "some comment7-8",
"postId": 8
},
{
"id": 9,
"body": "some comment9-1",
"postId": 1
}
]
http://192.168.123.234:8080/profile
{
"name": "typicode1"
}
● Relationshipsのテスト
http://192.168.123.234:8080/posts?_embed=comments
[
{
"id": 1,
"title": "json-server1",
"author": "typicode1",
"comments": [
{
"id": 1,
"body": "some comment1-1",
"postId": 1
},
{
"id": 3,
"body": "some comment3-1",
"postId": 1
},
{
"id": 9,
"body": "some comment9-1",
"postId": 1
}
]
},
{
"id": 2,
"title": "json-server2",
"author": "typicode2",
"comments": [
{
"id": 2,
"body": "some comment2-2",
"postId": 2
}
]
},
{
"id": 5,
"title": "json-server5",
"author": "typicode1",
"comments": [
{
"id": 5,
"body": "some comment5-5",
"postId": 5
}
]
},
{
"id": 8,
"title": "json-server8",
"author": "typicode2",
"comments": [
{
"id": 7,
"body": "some comment7-8",
"postId": 8
}
]
}
]
● Operatorsのテスト
http://192.168.123.234:8080/posts?id_ne=1
[
{
"id": 2,
"title": "json-server2",
"author": "typicode2"
},
{
"id": 5,
"title": "json-server5",
"author": "typicode1"
},
{
"id": 8,
"title": "json-server8",
"author": "typicode2"
}
]
とりあえず、手間要らずで簡単に動かしたい場合は JSON Serverが一番だなあ。
pi@raspberrypi:~ $ json-server -v
0.15.1
pi@raspberrypi:~ $ json-server
json-server [options] <source>
Options:
--config, -c Path to config file [default: "json-server.json"]
--port, -p Set port [default: 3000]
--host, -H Set host [default: "localhost"]
--watch, -w Watch file(s) [boolean]
--routes, -r Path to routes file
--middlewares, -m Paths to middleware files [array]
--static, -s Set static files directory
--read-only, --ro Allow only GET requests [boolean]
--no-cors, --nc Disable Cross-Origin Resource Sharing [boolean]
--no-gzip, --ng Disable GZIP Content-Encoding [boolean]
--snapshots, -S Set snapshots directory [default: "."]
--delay, -d Add delay to responses (ms)
--id, -i Set database id property (e.g. _id) [default: "id"]
--foreignKeySuffix, --fks Set foreign key suffix (e.g. _id as in post_id)
[default: "Id"]
--quiet, -q Suppress log messages from output [boolean]
--help, -h Show help [boolean]
--version, -v Show version number [boolean]
Examples:
json-server db.json
json-server file.js
json-server http://example.com/db.json
https://github.com/typicode/json-server
Missing <source> argument
●ラズパイ上でラズパイの IPアドレスを確認する方法
ifconfig | grep inet
pi@raspberrypi:~ $ ifconfig | grep inet
・2017/10/03
Raspberry Piに Avahi Bonjourサービスを入れて、ホスト名で接続できる様にする方法
ラズパイに Bonjourサービスを入れて IPアドレスが分からなくてもホスト名で簡単に接続する方法
・2018/04/20
Raspberry Piや Jetson NANO等をネットワークに接続した場合の IPアドレスの便利ツール xfinder
DHCPで自動で IPアドレスが割り当てられる場合に、ワンボードマイコンの IPアドレスを調べる場合に便利
Tags: [Raspberry Pi], [電子工作], [セキュリティ]
●関連するコンテンツ(この記事を読んだ人は、次の記事も読んでいます)
Raspberry Piで Googleの Go言語をインストールして動かす
Raspberry Piで Googleの Go言語をインストールして動かす
Raspberry Piで Googleの Go言語 + Gin Webフレームワークをインストールして Webアプリを作る
Raspberry Piで Googleの Go言語 + Gin Webフレームワークをインストールして Webアプリを作る
Raspberry Piで WebAPIモックサーバー Stubcellを動かしてみるテスト
Raspberry Piで APIモックサーバー Stubcellを動かしてみるテスト
Raspberry Piで WebAPIモックサーバー swagger-nodeを動かしてみるテスト
Raspberry Piで APIモックサーバー swagger-nodeを動かしてみるテスト
Raspberry Piに PHP 7.2を公式リポジトリからサクッと apt-getでインストールする方法
PHP7.2をラズパイ公式リポジトリから Raspberry Piに簡単にインストールする方法
Raspberry Pi 3の Linuxコンソール上で使用する各種コマンドまとめ
ラズパイの Raspbian OSのコマンドラインで使用する便利コマンド、負荷試験や CPUシリアル番号の確認方法等も
Raspberry Piに Avahi Bonjourサービスを入れて、ホスト名で接続できる様にする方法
ラズパイに Bonjourサービスを入れて IPアドレスが分からなくてもホスト名で簡単に接続する方法
Raspberry Piや Jetson NANO等をネットワークに接続した場合の IPアドレスの便利ツール xfinder
DHCPで自動で IPアドレスが割り当てられる場合に、ワンボードマイコンの IPアドレスを調べる場合に便利
[HOME]
|
[BACK]
リンクフリー(連絡不要、ただしトップページ以外は Web構成の変更で移動する場合があります)
Copyright (c)
2019 FREE WING,Y.Sakamoto
Powered by 猫屋敷工房 & HTML Generator
http://www.neko.ne.jp/~freewing/raspberry_pi/raspberry_pi_webapi_mock_json_server/