3

I'm pretty sure doing everything correctly. I'm using these version:

"axios": "^0.24.0",
"json-server": "^0.17.0",

I've followed the official doc. I've db.json in the root folder itself.

{
"users": [
    {
      "ID": 1,
      "Username": "mike",
      "Password": "User1Password"
    },
    {
      "ID": 2,
      "Username": "robert",
      "Password": "User2Password"
    }
  ]
}

I'm running json-server with this command: json-server --watch db.json --port 4000

Whenever I hit http://localhost:4000/users I'm served with this:

  \{^_^}/ hi!

  Loading db.json
  Done

  Resources
  http://localhost:4000/posts
  http://localhost:4000/comments
  http://localhost:4000/profile

  Home
  http://localhost:4000

  Type s + enter at any time to create a snapshot of the database
  Watching...

  GET /users 404 4.800 ms - 2

But rest of the end points like:

http://localhost:4000/posts
http://localhost:4000/comments
http://localhost:4000/profile

are working absolutely fine. Please help.

Tanzeel
  • 4,174
  • 13
  • 57
  • 110

4 Answers4

1

According to @user2740650
You said db.json is in src folder. What matters is that it's in the same folder where you started the server. It sounds like it created a default db.json somewhere else and is using that.

Second Scenario
move your db.json file into the Public folder and calling it by: axios.get('db.json') .then(//...)

Homezonic
  • 234
  • 2
  • 6
1

Copying my own comment to an answer as requested:

You said db.json is in the src folder. What matters is that it's in the same folder where you started the server. It sounds like it created a default db.json somewhere else and is using that.

user2740650
  • 1,723
  • 12
  • 19
  • can you plz look into this also: https://stackoverflow.com/questions/69777098/react-and-json-server-getting-request-failed-with-status-code-500 – Tanzeel Oct 30 '21 at 05:47
1

I had the same issue. Look at your JSON-server console output for the path to the version of db.json it is loading when you start the server. Use that file as your working copy. For me, the path was in the topmost folder, parent folder of src and public.

Otherwise it creates db.json in that path with the default data:

"posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
     { "id": 1, "body": "some comment", "postId": 1 }
    ],
  "profile": { "name": "typicode" }
}
Nathan A
  • 11
  • 3
0

The reason why the rest of the end points work is because when you install the json-sever it adds a default db.json inside your public folder having the following endpoints:

http://localhost:4000/posts
http://localhost:4000/comments
http://localhost:4000/profile

Default db.json will look like

{
    "posts": [{
        "id": 1,
        "title": "json-server",
        "author": "typicode"
    }],
    "comments": [{
        "id": 1,
        "body": "some comment",
        "postId": 1
    }],
    "profile": {
        "name": "typicode"
    }
  }

You can remove this data and add users collection as in your case.

Anne
  • 16
  • 3