Sunday, August 26, 2018

How to work with fake REST API?


Sometimes it is necessary to work with fake REST API for practice projects. To achieve such necessity we can implement JSON Server as follows:

npm install --save json-server

the above command is to install json-server package on nodejs project. Then next we can create a json file name db.json and can add json data as follows:

{
    "users": [
        {"id": "23", "firstName": "Bill", "age": 20},
        {"id": "44", "firstName": "Samantha", "age": 30}
    ]
}

Now we need to add script inside the package.json file with this text "json:server": "json-server --watch db.json" and full scripts as follows:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "json:server": "json-server --watch db.json"
  },


To run json server we can execute the command as follows:

npm run json:server

We can now access the db.json file's contents using the server url http://localhost:3000/users. 

To know more: https://github.com/typicode/json-server

1 comment:

  1. Do you understand there is a 12 word phrase you can speak to your partner... that will induce deep feelings of love and impulsive appeal for you buried within his chest?

    That's because deep inside these 12 words is a "secret signal" that triggers a man's impulse to love, worship and protect you with his entire heart...

    =====> 12 Words Who Fuel A Man's Desire Instinct

    This impulse is so built-in to a man's genetics that it will make him try better than ever before to do his best at looking after your relationship.

    Matter of fact, triggering this mighty impulse is so binding to achieving the best possible relationship with your man that once you send your man one of these "Secret Signals"...

    ...You'll immediately notice him expose his soul and heart to you in a way he's never experienced before and he will recognize you as the only woman in the galaxy who has ever truly fascinated him.

    ReplyDelete