Make sure you have Node install on your machine as we are going to use npm command.
Install JSON-SERVER using below command. I prefer to install it at global level using below command.
npm install -g json-server
Once install check it is installed properply using below command
PS C:\Windows\system32> json-server
bin.js [options] <source>
Options:
-c, --config Path to config file
[default: "json-server.json"]
-p, --port Set port [default: 3000]
-H, --host Set host [default: "localhost"]
-w, --watch Watch file(s) [boolean]
-r, --routes Path to routes file
-m, --middlewares Paths to middleware files [array]
-s, --static 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]
-S, --snapshots Set snapshots directory [default: "."]
-d, --delay Add delay to responses (ms)
-i, --id Set database id property (e.g. _id)
[default: "id"]
--foreignKeySuffix, --fks Set foreign key suffix (e.g. _id as in post_id)
[default: "Id"]
-q, --quiet Suppress log messages from output [boolean]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Examples:
bin.js db.json
bin.js file.js
bin.js http://example.com/db.json
https://github.com/typicode/json-server
Missing <source> argument
PS C:\Windows\system32>
Now lets create a folder assuming it as our db for all the CRUD operation
C:\json-server
Now create a file called as db.json and finally start our json-server using below command.
C:\json-server> json-server --watch db.json
PS C:\json-server> json-server --watch db.json
\{^_^}/ hi!
Loading db.json
SyntaxError: Malformed JSON in file: db.json
Unexpected token , in JSON at position 145
at FileAsync.parse [as deserialize] (<anonymous>)
at C:\Users\Siddhartha\AppData\Roaming\npm\node_modules\json-server\node_modules\lowdb\adapters\FileAsync.js:41:35
PS C:\json-server> json-server --watch db.json
\{^_^}/ hi!
Loading db.json
Done
Now lets create some data by hand by updating db.json files
1- db.json :-
{
"posts": [
{
"id": 1,
"title": "json-server",
"author": "typicode"
}
],
"comments": [
{
"id": 1,
"body": "some comment",
"postId": 1
}
],
"profile": {
"name": "typicode"
}
}
Now lets perform CRUD operation using POSTMAN tool.
1- POST operation
post data
{
"title": "sid-json-server",
"author": "sid-typicode"
}
2- Get Operation
Check above POST has created a values in db.json
[
{
"id": 1,
"title": "json-server",
"author": "typicode"
},
{
"title": "sid-json-server",
"author": "sid-typicode",
"id": 2
}
]
3- Update Operation
Put
{
"title": "sid-json-server-updated",
"author": "sid-typicode-updated",
"id": 2
}
4- Delete Operation
Delete
You can also check the ui using below url
For Documentation refer to below screen
Hi, could you please help me, what was the reason of this command error? Unexpected token , in JSON at position 145
I have a similar one, but at position 115. How to solve it?
1
PS C:\json-server> json-server –watch db.json
\{^_^}/ hi!
Loading db.json
SyntaxError: Malformed JSON in file: db.json
Unexpected token , in JSON at position 145
at FileAsync.parse [as deserialize] ()
at C:\Users\Siddhartha\AppData\Roaming\npm\node_modules\json-server\node_modules\lowdb\adapters\FileAsync.js:41:35
PS C:\json-server> json-server –watch db.json
\{^_^}/ hi!
Loading db.json
Done
LikeLike
Check you have extra , character that is not expected in the db.json file remove it and reconfirm
LikeLike