How to run JSON Server on a local project folder

Simply run the following command

npx json-server --port 3001 --watch db.json

Please note that I’ve set the port number to 3001 instead of the default 3000 as react js uses the port number 3000 . Put db.json file in the root of your react js project with some json data. Then those data can be accessible via this link: http://localhost:3001/json_root/ where “json_root” is the name of the root node in that JSON file.

An alternative is to install json-server via npm

npm install json-server --save-dev

then simply run the server by the following command

npm run server

If you follow this route and wish to use a custom port then add the following line to the package.json file under the scripts section

"server": "json-server -p3001 --watch db.json"

The scripts section now should look somewhat like the following

{
  // ... 
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "server": "json-server -p3001 --watch db.json"
  },
}

Leave a Comment

Your email address will not be published. Required fields are marked *