Json-server installation and use of

  • First, tell us about what is json-server, what use is actually very simple:
    JSON-Server is a Node module, run the Express server, you can specify a json api file as a data source.
  • Next we need a global json-server installed on the computer:
    npm install -g json-server
  • Create a new folder, initialization json-server data, and then enter into the folder on the line all the way round :()
    nPM heat
  • Json-server installation in the initialization of the project:
    npm install json-server --save
  • Then we in the new folder you will see a package.json file, and there is "json-server": "^ 0.15.1" words, and then create a db.json file in the current directory, under the present document write your own json data: for example, my data:
    {
        "users":[
            {
                "name":"Henry",
                "phone":"333-444-555",
                "email":"[email protected]",
                "id":1,
                "age":33,
                "companyId":1
            },{
                "name":"Bucky",
                "phone":"333-444-555",
                "email":"[email protected]",
                "id":2,
                "age":34,
                "companyId":2
            },{
                "name":"Emily",
                "phone":"333-444-555",
                "email":"[email protected]",
                "id":3,
                "age":43,
                "companyId":3
            }
        ],"companies":[
                {
                    "id":1,
                    "name":"Apple",
                    "description":"Apple is good"
                },{
                    "id":2,
                    "name":"Miciosoft",
                    "description":"Miciosoft is good"
                },{
                    "id":3,
                    "name":"Google",
                    "description":"Google is good"
                }
            ]
    }
  • Then we can run our local json server:
    json-server --watch db.json

    In fact, we can modify the data inside package.json changes run the command:

    {
      "name": "jsonserver",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "json:server": "json-server --watch db.json",
        "json:server:remote":"json-server http://jsonplaceholder.typicode.com/db"
      },
      "author": "",
      "license": "ISC",
      "dependencies": {
        "json-server": "^0.15.1"
      }
    }

    After this modification, we only need to enter the command line:

    run locally:

    npm run json:server

    Remote mode:

    npm run json:server:remote
  • Here our json-server build better
  • Of course, what kind of access the json data, and how to call the relevant interface, direct access to json-server official website
    https://www.npmjs.com/package/json-server

Guess you like

Origin www.cnblogs.com/zhuzhu520/p/11458282.html