如何使用JSON Server|获取本地db.json的404

ars1skjm  于 2023-10-21  发布在  其他
关注(0)|答案(5)|浏览(171)

我很确定每件事都做对了。我使用这个版本:

  1. "axios": "^0.24.0",
  2. "json-server": "^0.17.0",

我已经看过官方文件了。我在根文件夹中有db.json

  1. {
  2. "users": [
  3. {
  4. "ID": 1,
  5. "Username": "mike",
  6. "Password": "User1Password"
  7. },
  8. {
  9. "ID": 2,
  10. "Username": "robert",
  11. "Password": "User2Password"
  12. }
  13. ]
  14. }

我使用以下命令运行json-serverjson-server --watch db.json --port 4000
每当我点击http://localhost:4000/users时,我都会收到这个:

  1. \{^_^}/ hi!
  2. Loading db.json
  3. Done
  4. Resources
  5. http://localhost:4000/posts
  6. http://localhost:4000/comments
  7. http://localhost:4000/profile
  8. Home
  9. http://localhost:4000
  10. Type s + enter at any time to create a snapshot of the database
  11. Watching...
  12. GET /users 404 4.800 ms - 2

但其余的终点,如:

  1. http://localhost:4000/posts
  2. http://localhost:4000/comments
  3. http://localhost:4000/profile

工作得非常好请帮帮我

9rbhqvlz

9rbhqvlz1#

根据@user2740650

你说db.json在src文件夹里。重要的是,它位于启动服务器的同一个文件夹中。听起来它在其他地方创建了一个默认的db.json并正在使用它。

第二种情况

将db.json文件移动到Public文件夹中,并通过以下方式调用它:axios.get('db.json').then(//.)

kqqjbcuj

kqqjbcuj2#

根据要求,将我自己的评论添加到答案中:
你说db.jsonsrc文件夹里。重要的是,它位于启动服务器的同一个文件夹中。听起来它在其他地方创建了一个默认的db.json,并正在使用它。

m528fe3b

m528fe3b3#

我也有同样的问题。查看JSON服务器控制台输出,以获取启动服务器时加载的db.json版本的路径。使用该文件作为您的工作副本。对我来说,路径是在最上面的文件夹,src和public的父文件夹。
否则,它将在该路径中使用默认数据创建db.json:

  1. "posts": [
  2. { "id": 1, "title": "json-server", "author": "typicode" }
  3. ],
  4. "comments": [
  5. { "id": 1, "body": "some comment", "postId": 1 }
  6. ],
  7. "profile": { "name": "typicode" }
  8. }
6gpjuf90

6gpjuf904#

其余端点正常工作的原因是,当你安装json-sever时,它会在你的public文件夹中添加一个默认的db.json,它具有以下端点:

  1. http://localhost:4000/posts
  2. http://localhost:4000/comments
  3. http://localhost:4000/profile

默认的db.json看起来像

  1. {
  2. "posts": [{
  3. "id": 1,
  4. "title": "json-server",
  5. "author": "typicode"
  6. }],
  7. "comments": [{
  8. "id": 1,
  9. "body": "some comment",
  10. "postId": 1
  11. }],
  12. "profile": {
  13. "name": "typicode"
  14. }
  15. }

您可以删除此数据并添加用户集合,就像您的情况一样。

展开查看全部
gpfsuwkq

gpfsuwkq5#

同样的事情发生在我身上,我是我的情况下,我没有指定数据库路由是在一个名为数据库的文件夹,例如:json-server --watch db.json应该是:json-server --watch database/db.json

相关问题