为什么找不到“:服务器”和“:客户端”?

why ':server' and ':client' not found?

我正在尝试从 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": "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 install --save json-server> npx json-server --观看 server/db.json 并且我编辑了 "scripts": 部分如下:

"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 install --save concurrently 然后我在 "scripts":

"start": "npx concurrently \"npm run start:server\" \"npm run start:client\"",

然后我执行了 > npm install --save http-proxy-middleware 然后我创建了 setupProxy.js 并输入了以下代码:

const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports=function(app){
    app.use(createProxyMiddleware('/api',{
        target:'http://localhost:5000',
        pathRewrite:{'^/api':''}
    }))
}

然后我执行了 npm install,然后当我执行 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 运行 dev 或 npm start

对我有用 并在 setupProxy.js 文件

中使用 createProxyMiddleware 而不是代理