nodejs構建mock資料

來源:互聯網
上載者:User

標籤:sts   技術   內容   目錄   nod   git   pen   version   json格式   

Nodejs構建mock資料並通過rest api風格調用介面訪問資料

如果我們只有json格式的資料檔案,我們想通過訪問url方式調用居然資料

 

確保電腦安裝node環境

如果你沒有安裝好node環境請移步http://nodejs.cn/

 一、安裝json-server

 1.建立demo檔案

cd demo

 2.安裝json-server

 

npm install -S json-server

 

3.項目demo目錄下,建立一個 JSON 檔案data.json和一個package.json檔案

db.json內容如下

{  "posts": [    { "id": 1, "title": "json-server", "author": "typicode" }  ],  "comments": [    { "id": 1, "body": "some comment", "postId": 1 }  ],  "profile": { "name": "typicode" }}

 

package.json內容如下

 

{  "name": "rest-api-demo",  "version": "1.0.0",  "description": "",  "main": "index.js",  "scripts": {    "server": "json-server data.json",    "test": "..."  },  "keywords": [],  "author": "",  "license": "ISC",  "dependencies": {    "json-server": "^0.9.4"  }}

 

二、啟動服務

npm run server

 

三、調用rest 風格api

開啟瀏覽器

 

 

注意這個的資料擷取只是get方式請求擷取到的,如需POST、PUT、DELETE等 HTTP 方法的資料體進行解析請參考

https://github.com/ruanyf/jstraining/tree/master/demos#rest-api

 

nodejs構建mock資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.