无法使用 json-server 并一起做出反应
unable to use json-server and react together
我正在尝试在我已经通过 React 设置的项目目录中使用 json-server:npx create-react-app new-app
。在 new-app
目录,我有一个文件 db.json
,其中包含一些内容:
{
"notes": [
{
"id": 1,
"content": "first content",
},
{
"id": 2,
"content": "content 2",
}
]
}
我还安装了 json-server npm install json-server --save-dev
。然后,我通过 运行 端口 3001 上的服务器:npx json-server --port 3001 --watch db.json
到目前为止一切运行顺利,我可以在 http://localhost:3001/notes
上看到注释文件。
此时,为了 packages.json
,在 "scripts" 下,我添加了这样的服务器配置:
{
"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"
}
}
根据我所关注的参考资料,这应该显示 http://localhost:3001 上的 React 的所有内容,但我得到的只是一个标题为 React App 的空白页面(也没有图标) .我的根目录如下(除了提到的以外没有变化):
谁能指出这里出了什么问题?或者,他们是如何做出反应并 json-server 正确设置的?
以这种方式设置时,public 中的 index.html 似乎没有使用 src 文件夹中的 index.js 文件(和其他 .js 文件) =]文件夹。
你想看的是网站,而不是 json 服务器的内容,我说得对吗?也许试试 http://localhost:3000.
我正在尝试在我已经通过 React 设置的项目目录中使用 json-server:npx create-react-app new-app
。在 new-app
目录,我有一个文件 db.json
,其中包含一些内容:
{
"notes": [
{
"id": 1,
"content": "first content",
},
{
"id": 2,
"content": "content 2",
}
]
}
我还安装了 json-server npm install json-server --save-dev
。然后,我通过 运行 端口 3001 上的服务器:npx json-server --port 3001 --watch db.json
到目前为止一切运行顺利,我可以在 http://localhost:3001/notes
上看到注释文件。
此时,为了 packages.json
,在 "scripts" 下,我添加了这样的服务器配置:
{
"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"
}
}
根据我所关注的参考资料,这应该显示 http://localhost:3001 上的 React 的所有内容,但我得到的只是一个标题为 React App 的空白页面(也没有图标) .我的根目录如下(除了提到的以外没有变化):
谁能指出这里出了什么问题?或者,他们是如何做出反应并 json-server 正确设置的?
以这种方式设置时,public 中的 index.html 似乎没有使用 src 文件夹中的 index.js 文件(和其他 .js 文件) =]文件夹。
你想看的是网站,而不是 json 服务器的内容,我说得对吗?也许试试 http://localhost:3000.