This time I will show you how json-server creates back-end data, and what are the precautions for how json-server creates back-end data. The following is a practical case, let's take a look.
During the development process, the front-end and back-end are separated or not separated. The interface is mostly developed with the page later, so it is very necessary to establish a rest APL interface to provide virtual data to the front-end, so here I use json- As a tool, server supports CORS and JSONP cross-domain requests, supports GET, POST, PUT, PATCH and DELETE methods, and also provides a series of query methods, such as limit, order, etc. Next, I wrote my own usage as Document
Installation
First you must have a node environment (you must have a node environment if you use json-server) and then install json- globally server
npm install json-server -g
Copy after login
After the installation is completed, check whether the global installation is successful
G:\demo\JavaScript\Vue\Vue one\project\my-project\Vue_two\my-project>json-server -h index.js [options] Options: --config, -c Path to config file [default: "json-server.json"] --port, -p Set port [default: 3000] --host, -H Set host [default: "0.0.0.0"] --watch, -w Watch file(s) [boolean] --routes, -r Path to routes file --middlewares, -m Paths to middleware files [array] --static, -s Set static files directory --read-only, --ro Allow only GET requests [boolean] --no-cors, --nc Disable Cross-Origin Resource Sharing [boolean] --no-gzip, --ng Disable GZIP Content-Encoding [boolean] --snapshots, -S Set snapshots directory [default: "."] --delay, -d Add delay to responses (ms) --id, -i Set database id property (e.g. _id) [default: "id"] --foreignKeySuffix, --fks Set foreign key suffix (e.g. _id as in post_id) [default: "Id"] --quiet, -q Suppress log messages from output [boolean] --help, -h Show help [boolean] --version, -v Show version number [boolean] Examples: index.js db.json index.js file.js index.js http://example.com/db.json https://github.com/typicode/json-server
Copy after login
Create a db.json directory in the project root directory, and then write the information
in package.json Add a line of commands to the scripts inside
"json": "json-server db.json --port 3003"
Copy after login
Execute the command in the project directory
npm run json
You will see such an interface in bash
> vue@1.0.0 json g:\demo\JavaScript\Vue\Vue one\project\my-project\Vue_two\my-project > json-server db.json --port 3003 \{^_^}/ hi! Loading db.json Done Resources http://localhost:3003/api Home http://localhost:3003 Type s + enter at any time to create a snapshot of the database
I believe I read this article You have mastered the case method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
The above is the detailed content of How to create back-end data with json-server. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn