nodejs构建mock数据

Nodejs构建mock数据并经过rest api风格调用接口访问数据node

若是咱们只有json格式的数据文件,咱们想经过访问url方式调用竟然数据git

 

确保电脑安装node环境github

若是你没有安装好node环境请移步http://nodejs.cn/npm

 1、安装json-serverjson

 1.新建demo文件api

cd demo浏览器

 2.安装json-serverpost

 

npm install -S json-server

 

3.项目demo目录下,新建一个 JSON 文件data.json和一个package.json文件url

db.json内容以下spa

{
  "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"

  }

}

 

2、启动服务

npm run server

 

3、调用rest 风格api

打开浏览器

 

 

注意这个的数据获取只是get方式请求获取到的,如需POSTPUTDELETE等 HTTP 方法的数据体进行解析请参考

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

相关文章
相关标签/搜索