Javascript 为什么';:服务器';和';:客户';找不到?
我正在尝试从db.json获取数据 这是我的db.json文件:Javascript 为什么';:服务器';和';:客户';找不到?,javascript,json,reactjs,react-hooks,npm-start,Javascript,Json,Reactjs,React Hooks,Npm Start,我正在尝试从db.json获取数据 这是我的db.json文件: { "posts": [ { "id": "react-hooks", "title": "React Hooks", "content": "The greatest thing since sliced bread!", "author": "ali" }, { "id": "react-fragments", "title": "Using React Fragments", "content
{
"posts": [
{ "id": "react-hooks", "title": "React Hooks", "content": "The greatest thing since sliced bread!", "author": "ali" },
{ "id": "react-fragments", "title": "Using React Fragments", "content": "Keeping the DOM tree clean!", "author": "ali" }
],
"users": [
{ "id": 1, "username": "ali", "password": "********" }
],
"themes": [
{ "id": 1, "primaryColor": "deepskyblue", "secondaryColor": "coral" },
{ "id": 2, "primaryColor": "orchid", "secondaryColor": "mediumseagreen" },
{ "id": 3, "primaryColor": "darkslategray", "secondaryColor": "slategray" }
]
}
在这之后,我执行了npm安装——保存json服务器和npx json服务器——查看服务器/db.json,并编辑了“脚本”:部分如下:
"start:server": "npx json-server --watch server/db.json",
"start:client": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
然后我执行了npm安装——并发保存,然后在脚本中添加了这一行:
"start": "npx concurrently \"npm run start:server\" \"npm run start:client\"",
然后我执行了npm安装——保存http代理中间件,然后我创建了setupProxy.js并输入以下代码:
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports=function(app){
app.use(createProxyMiddleware('/api',{
target:'http://localhost:5000',
pathRewrite:{'^/api':''}
}))
}
然后,我执行了npm安装,之后,当我执行npm start时,它显示以下错误:
The system cannot find the file :client.
[5] start:client exited with code 1
[2] The system cannot find the file :server.
[2] start:server exited with code 1
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! reacttutorial@0.1.0 start: `npx concurrently "npm run start:server" "npm run start:client"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the reacttutorial@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
有两个windows警报显示:找不到“:server”和“:client”
我已经通过执行npx create react app reacttutorial创建了全新的应用程序。我的想法快用完了。谁能帮我一下吗?看看这个回复
所以你加入了
"scripts": {
"dev": "concurrently \"npm run server\" \"npm run react\"",
"server": "npx json-server --watch server/db.json --port 4000 --routes server/routes.json",
"react": "react-scripts start",
...}
并使用npm run dev或npm start
为我工作
并且在setupProxy.js文件中使用createProxyMiddleware代替proxyWow,那是很久以前的事了,我已经在不同的终端上解决了这个问题。但我会检查你的解决方案。看到有人回答这样一个老问题真是太酷了,我想谢谢你,不经检查就接受你的回答。又来了