Tanzeel
Tanzeel

Reputation: 4998

How to use json-server | Getting 404 for local db.json

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.

Upvotes: 3

Views: 10747

Answers (6)

gee77
gee77

Reputation: 13

This happened to me. There was another service "OneApp.IGCC.WinService.exe" was listening on port 5000(on which I ran the json server). I changed the port json server was running on. Then it worked fine.

Upvotes: 1

M Gh
M Gh

Reputation: 1

The same thing happened to me, I'm my case I wasn't specifying the db route which was in a folder named database, for example: json-server --watch db.json should be: json-server --watch database/db.json

Upvotes: 0

Nathan A
Nathan A

Reputation: 11

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" }
}

Upvotes: 1

Anne
Anne

Reputation: 26

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.

Upvotes: 0

user2740650
user2740650

Reputation: 1753

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.

Upvotes: 1

Homezonic
Homezonic

Reputation: 254

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(//...)

Upvotes: 3

Related Questions