2

I am working with json-server. I have installed it with npm install -g json-server and am watching using

json-server --watch db.json

I have added db.json file in app folder with below data

{
    "product": [
      {
        "userId": 1,
        "firstName": "Krish",
        "lastName": "Lee",
        "phoneNumber": "123456",
        "emailAddress": "[email protected]"
      },
      {
        "userId": 5,
        "firstName": "jone",
        "lastName": "mac",
        "phoneNumber": "111111111",
        "emailAddress": "[email protected]"
      }
    ]
  }

but only http://localhost:3000/ is working. It shows

Congrats!
You're successfully running JSON Server
✧*。٩(ˊᗜˋ*)و✧*。

Resources
/posts 1x
/comments 1x
/profile object
To access and modify resources, you can use any HTTP method:

GET POST PUT PATCH DELETE OPTIONS

undefined
Documentation
README

To replace this page, create a ./public/index.html file.

http://localhost:3000/product is not working. How can I fix this issue?

5
  • Do you see any output from json-server when it starts up? I see it list the resource endpoints, including http://localhost:3000/product which works fine. Your posted JSON is malformed, with an invalid trailing comma, but I assume that's an artifact of cutting it down to post (because json-server errors out if given malformed JSON). Please give a minimal reproducible example. Commented Sep 16, 2020 at 15:20
  • @jonrsharpe - I have updated my question. Cound you please check. Commented Sep 16, 2020 at 15:51
  • They're the defaults, presumably you also see "Oops, db.json doesn't seem to exist"? Post (and read!) all of the output you're given. Commented Sep 16, 2020 at 15:53
  • And have you double-checked the actual content of the db.json it's loading? The first time you run it, if it can't find the file you asked for, it writes out its own defaults for you to edit. When that happened, it would have told you it was doing that. Commented Sep 16, 2020 at 15:56
  • @jonrsharpe - It's working now. another db.json was getting created in different level. I didn't notice that before. Now able to post my own json and get the url browse. Commented Sep 16, 2020 at 16:02

4 Answers 4

2

The first time you try to run json-server@0, if it can't find the supplied source, load.js writes out the default example for you:


> json-server --watch db.json


  \{^_^}/ hi!

  Loading db.json
  Oops, db.json doesn't seem to exist
  Creating db.json with some default data

  Done

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

  Home
  http://localhost:3000

On subsequent runs, because the file now exists, the server seems to be starting up just fine but isn't using the db.json file you think it is. For that reason, this seems like a bit of a misfeature.

I opened a PR to require this behaviour to be explicitly requested; based on the prerelease -alpha versions it looks like this automatic creation won't be a feature of json-server@1.

Sign up to request clarification or add additional context in comments.

Comments

1

db.json is supposed to be set up in in the root folder? you indicated you put yours in the app folder.

Comments

0

At initial the JSON-server must be started from the root folder. ex : if you are using VS code editor click on the folder that consist the data in JSON format and right click on that folder and open it in integrated terminal and try : > json-server --watch db.json and it will be working fine.

In case of directly calling the Json-server, it will initialise the default data on localhost:3000

Comments

0

You have to select the PATH of db.json file like below

"serve-json": "json-server --watch ./src/db.json --port 5000"

now use the comments in terminal npm run serve-json

it will navigate port 5000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.