How to install JSON-SERVER in windows and perform CRUD operation

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

http://localhost:3000/

For Documentation refer to below screen

https://github.com/typicode/json-server

About shdhumale

• Having professional experience in development of various applications on different Web based Application and Client Server Application. • Strong understanding of Spring,Spring LDAP, Spring Security, GWT(Google Web Tool), Ext- GWT, SOAP Technology (Apache Axis, Apache CXF RS,WS), Thrift, Java web Start,Hibernate, Ajax, Portal, Portlet, Jersey Restful Services, Java OSGI Frame, Shibboleth Single Sing on Architecture, Core Java, Struts, Swing, and J2EE Technologies like JSP, Servlet, JDBC and Java Beans, EJB (Both Sesssion and Entity Bean), Android Mobile Development, Apache Kafka. Service Mesh, Microservice Architecture, Docker, Kubernetes, Helm Charts, ELK EFK Stack, DaTree, Hybrid Mobile development using Ionic Frame work. • Sound knowledge of Front End Java frame work like Angular 6 and React. • Sound knowledge of integrating SSO Circle Single Sign On, ADFS integration.
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to How to install JSON-SERVER in windows and perform CRUD operation

  1. Tanya says:

    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

    Like

Leave a comment