简体   繁体   中英

Integrating json-server with a react app deployed using aws amplify

I have developed a react app with mock backend developed using json-server and had successfully tested in my local using the npm start command and was working as expected. Then i moved onto deploying the app in aws using aws-amplify and the deployment was also success. One issue that i face now is, how to make the json-server up in the aws amplify, since my all backend calls are failing now as json-server is not up. PFB the steps that i tried to integrate json-server in aws-amplify 1.In the build settings of aws-amplify i tried giving the below commands to ensure that a json-server is installed before the build and provided the command to start the json-server after '&&' to ensure the json-sever is made up after the sequence of build

  • npm install -g json-server
  • npm run build && npx json-server --watch db.json --port 3001

Pls. let me know is there a way in which i can make the json-server up along with the aws-amplify so that my backend calls are success. If aws-amplify is not the right option which aws service should i use to make my backend working. Can i deploy a mock-service using json-server in aws ec2 and try to invoke that service from my UI.

if you are trying to run multiple applications when you 'npm start', use concurrently npm package (for local machine testing only, not as solution to run on a remote host like AWS etc).

Then you can amend your package.json file to run multiple applications as below example;

  "scripts": {
    "start": "concurrently --kill-others \"react-scripts start\" \"npx json-server --watch data/db.json --port 8000\"",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

Create a separate JSON server, then connect to it, you can do this with Heroku which offers free hosting for personal/development projects.

I have also done this for one of my previous projects on GH; https://github.com/rbhachu/the-mojo-blog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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