介紹兩大神器!——使用json-server和faker.js模擬REST API

今天發現了一個神器——json-server!在他的幫助下能夠在很短的時間內搭建一個Rest API, 而後就可讓前端在不依賴後端的狀況下進行開發啦!javascript

關於什麼是RESTful API:
《RESTful API 設計指南》—— 阮一峯
http://www.ruanyifeng.com/blo...html

JSON-Server

簡單來講,JSON-Server是一個Node模塊,運行Express服務器,你能夠指定一個json文件做爲api的數據源。前端

舉個例子:
咱們如今想作一個app,用來管理客戶信息,實現簡單的CRUD功能(create/retrieve/update/delete),好比:java

  • 獲取客戶信息git

  • 增長一個客戶github

  • 刪除一個客戶npm

  • 更新客戶信息json

好啦,接下來咱們就使用json-server完成這一系列動做吧!後端

安裝JSON-Server

npm install -g json-server   //osx系統加'sudo'

新建一個文件夾同時cd它:api

mkdir customer-manager && cd customer-manager

新建一個json文件,而後存放一點數據進去:

touch customers.json
{
  "customers": [
    { "id": 1, "first_name": "John", "last_name": "Smith",  "phone": "219-839-2819" }
  ]
}

開啓json-server功能

全部你要作的事情只是讓json-server指向這個customers.json就ok啦!

json-server customers.js

而後出現這個提示就ok啦!
圖片描述

另外,JSON-Server很酷的一點就是支持各類GET/POST/PUT/DELETE的請求。
看幾個例子:

//GET
fetch('http://localhost:3000/tasks/')
  .then(function(response) {
    return response.json()
  }).then(function(json) {
    console.log('parsed json: ', json)
  }).catch(function(ex) {
    console.log('parsing failed: ', ex)
  });


//POST
fetch('http://localhost:3000/tasks/', {
  method: 'post',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
       "title":   "Add a blogpost about Angular2",
       "dueDate": "2015-05-23T18:25:43.511Z",
       "done": false
   })
}).then(function(response) {
      return response.json()
    }).then(function(json) {
      console.log('parsed json: ', json)
    }).catch(function(ex) {
      console.log('parsing failed: ', ex)
    });
    
    
//PUT
fetch('http://localhost:3000/tasks/1', { //在url後面指定下id就好
  method: 'put',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
       "done": true
   })
}).then(function(response) {
      return response.json()
    }).then(function(json) {
      console.log('parsed json: ', json)
    }).catch(function(ex) {
      console.log('parsing failed: ', ex)
    });
    
    
//DELETE
fetch('http://localhost:3000/tasks/1', {
method: 'delete'
}).then(function(response) {
   return response.json()
 }).then(function(json) {
   console.log('parsed json: ', json)
 }).catch(function(ex) {
   console.log('parsing failed: ', ex)
 });

JSON-Server基本就是這樣啦!接下來介紹另外一個神器~

Faker.js

若是要本身瞎編API數據的話也是比較煩惱,用faker.js就能夠輕鬆解決這個問題啦!他能夠幫助你自動生成大量fake的json數據,做爲後端數據~

安裝faker.js

仍是使用npm來安裝faker.js:

npm install faker

如今咱們用javascript生成一個包含50個客戶數據的json文件:

//customers.js
var faker = require('faker')

function generateCustomers () {
  var customers = []

  for (var id = 0; id < 50; id++) {
    var firstName = faker.name.firstName()
    var lastName = faker.name.firstName()
    var phoneNumber = faker.phone.phoneNumberFormat()

    customers.push({
      "id": id,
      "first_name": firstName,
      "last_name": lastName,
      "phone": phoneNumber
    })
  }

  return { "customers": customers }
}

// 若是你要用json-server的話,就須要export這個生成fake data的function
module.exports = generateCustomers

而後讓json-server指向這個js文件:

json-server customers.js

這樣你就能夠在http://localhost:3000/customers裏看到50個客戶數據了。
更多faker.js屬性能夠查看這裏:
https://github.com/marak/Fake...

好啦,基本就是這樣啦,happy coding!

相關文章
相關標籤/搜索