简体   繁体   English

无法使用 json-server 并一起做出反应

[英]unable to use json-server and react together

I'm trying to use json-server in a project directory that I already set up with React via: npx create-react-app new-app .我正在尝试在我已经通过 React 设置的项目目录中使用 json-server : npx create-react-app new-app In the new-app directory, I have a file db.json with some content:在 new-app 目录中,我有一个包含一些内容的文件db.json

{
  "notes": [
    {
      "id": 1,
      "content": "first content",
    },
    {
      "id": 2,
      "content": "content 2",
    }
  ]
}

I also have the json-server installed with npm install json-server --save-dev .我还使用npm install json-server --save-dev Then, I ran the server on port 3001 via: npx json-server --port 3001 --watch db.json and everything so far runs smoothly and I can see the notes file on http://localhost:3001/notes .然后,我通过以下方式在端口 3001 上运行服务器: npx json-server --port 3001 --watch db.json到目前为止一切运行顺利,我可以在http://localhost:3001/notes上看到注释文件。

At this point, to packages.json , under "scripts", I added a server configuration as such:在这一点上,到packages.json ,在“脚本”下,我添加了一个服务器配置,如下所示:

{
  "name": "new-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "server": "json-server -p3001 --watch db.json" // ADDED THIS
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "json-server": "^0.16.1"
  }
}

According to the reference that I was following, this was supposed to show all the stuff from react on http://localhost:3001 , but what I get is just a blank page with the title React App (no icon either).根据我所关注的参考,这应该显示http://localhost:3001上 react 的所有内容,但我得到的只是一个标题为 React App 的空白页面(也没有图标)。 My root directory is as follows (no changes other than ones mentioned):我的根目录如下(除了提到的没有变化):

https://imgur.com/a/MR0NYrr https://imgur.com/a/MR0NYrr

Could anyone point out what went wrong here?谁能指出这里出了什么问题? Or alternatively, how they managed to get react and json-server set up correctly?或者,他们如何设法正确设置 react 和 json-server ?

When set up this way, it doesn't seem like the index.js file (and other .js files) in the src folder is being used by the index.html in the public folder.以这种方式设置时,公共文件夹中的 index.html 似乎没有使用 src 文件夹中的 index.js 文件(和其他 .js 文件)。

Am I right that you want to see the website, not the content of the json-server?你想看网站,而不是 json-server 的内容,我说得对吗? Maybe try http://localhost:3000 .也许尝试http://localhost:3000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM