GithubHelp home page GithubHelp logo

161115_react-news's Introduction

react app 笔记

REST API

  • restful api(rest风格API)

  • restless api(非rest风格API)

  • 使用json-server

    • npm install json-server -g
    • 创建服务器端数据库文件 server/db.json
      {
        "posts": [
          { "id": 1, "title": "json-server", "author": "typicode" }
        ],
        "comments": [
          { "id": 1, "body": "some comment", "postId": 1 }
        ],
        "profile": { "name": "typicode" }
      }
      
    • 编码测试
      /*
      测试restful API的CRUD请求
       */
      const axios = require('axios')
      
      /*
      测试查询请求: GET请求
       */
      function testQuery() {
        //查询所有
        axios.get('http://localhost:3000/posts')
          .then(function (response) {
            console.log(response.data)  //所有数据的数组
          })
        //查询一条
        axios.get('http://localhost:3000/posts/2')
          .then(function (response) {
            console.log(response.data) //指定id数据的对象
          })
      }
      
      /*
       测试添加请求: POST请求
       */
      function testAdd() {
        const obj = {"title": "atguigu", "author": "JACK"}
        axios.post('http://localhost:3000/posts', obj)
          .then(function (response) {
            console.log(response.data)  //
          })
      }
      
      /*
       测试更新请求: PUT请求
       */
      function testUpdate() {
        const obj = {"title": "beijin", "author": "xfzhang"}
        axios.put('http://localhost:3000/posts/4', obj)
          .then(function (response) {
            console.log(response.data)
          })
      }
      
      /*
       测试删除请求: DELETE请求
       */
      function testDelete() {
        axios.delete('http://localhost:3000/posts/4')
          .then(function (response) {
            console.log(response.data)
          })
      }
      
      testDelete()
      // testUpdate()
      // testAdd()
      // testQuery()
      

161115_react-news's People

Contributors

zxfjd3g avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.