json-server 使用全攻略(一)

安装

$ npm install -g json-server

资源的使用

1. json文件源

$ json-server db.json
$ json-server --host 192.168.1.102 -p 6000 db.json

注意:如不使用--host则只绑定在localhost上

  1. 必须是对象格式
  2. 需要有id,名字默认是id
{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ],
  "profile": { "name": "typicode" }
}

2. url 源

$ json-server http://example.com/file.json
$ json-server http://jsonplaceholder.typicode.com/db
  1. 必须是对象格式
  2. 需要有id,名字默认是id

3. js模块源

格式:

// index.js
module.exports = () => {
  const data = { users: [] }
  // Create 1000 users
  for (let i = 0; i < 1000; i++) {
    data.users.push({ id: i, name: `user${i}` })
  }
  return data
}

启动:

$ json-server index.js

过滤方式(Filter)

GET /posts?title=json-server&author=typicode
GET /posts?id=1&id=2
GET /comments?author.name=typicode

分页方式(Paginate)

GET /posts?_page=7
GET /posts?_page=7&_limit=20

排序

GET /posts?_sort=views&_order=asc
GET /posts/1/comments?_sort=votes&_order=asc
// 多排序
GET /posts?_sort=user,views&_order=desc,asc

切片(Slice)

GET /posts?_start=20&_end=30
GET /posts/1/comments?_start=20&_end=30
GET /posts/1/comments?_start=20&_limit=10

全文检索(Full-text search)

GET /posts?q=internet

静态网页服务器

  1. 直接使用 ./public 文件夹下的静态资源即可
mkdir public
echo 'hello world' > public/index.html
json-server db.json
  1. 指定静态资源:
json-server db.json --static ./some-other-dir

映射成为新的url资源

  1. Create a routes.json file:
{
  "/api/*": "/$1",
  "/:resource/:id/show": "/:resource/:id",
  "/posts/:category": "/posts?category=:category",
  "/articles\\?id=:id": "/posts/:id"
}
  1. Start json-server:
json-server db.json --routes routes.json
  1. 资源映射后的效果:
/api/posts # → /posts
/api/posts/1  # → /posts/1
/posts/1/show # → /posts/1
/posts/javascript # → /posts?category=javascript
/articles?id=1 # → /posts/1

定制请求和响应

// hello.js
module.exports = (req, res, next) => {
  res.header('X-Hello', 'World')
  next()
}

启动方式:

json-server db.json --middlewares ./hello.js
json-server db.json --middlewares ./first.js ./second.js

启动选项列表:

以下选项,多数可以使用json-server的配置文件(./json-server.json)完成配置

json-server [options] 

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]

一个json-server.json的配置示例:

{
  "port": 8080,
  "id": "_id",
  "delay": 1000,
  "routes": "routes.json"
}

你可能感兴趣的:(json-server 使用全攻略(一))