0

i am trying to fetch data from the db.json.

here is my db.json file:

{
    "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" }
    ]
  }

after that i have executed > npm install --save json-server and > npx json-server --watch server/db.json and i have edited the "scripts": part as followed:

"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"

then i have excuted > npm install --save concurrently and then i have added that line in the "scripts":

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

then i have executed > npm install --save http-proxy-middleware and after that i have created setupProxy.js and put the following code:

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

then i excuted npm install and after that when i execute npm start it shows the following error:

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.

and there are two windows alerts which are saying that ':server' and ':client' cannot be found.

i have created whole new app by executing npx create-react-app reacttutorial. i am just running out of ideas. can anyone help me on that please?

ali
  • 135
  • 1
  • 13

1 Answers1

1

look at this reply React scripts fails while i run concurrently with nodemon

so you put in

"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",
  ...}

and use npm run dev instead or npm start

worked for me and also use createProxyMiddleware instead of proxy in setupProxy.js file

punund
  • 4,321
  • 3
  • 34
  • 45
user12461576
  • 26
  • 1
  • 2
  • Wow, that’s a long time ago and I have solved it executing in different terminal. But I will check your solution. It’s cool to see someone answered such an old question and I would like to thank you and accept ur answer without checking. Thanx again – ali Dec 13 '20 at 21:22