Skip to content

Instantly share code, notes, and snippets.

@arilotter
Created July 11, 2018 14:09
Show Gist options
  • Select an option

  • Save arilotter/cb64ace44dbe1022304a64b831af988f to your computer and use it in GitHub Desktop.

Select an option

Save arilotter/cb64ace44dbe1022304a64b831af988f to your computer and use it in GitHub Desktop.
create-react-app backend example

A backend with create-react-app

Modify your folder structure so you have something like

your_project
  | frontend ( your create-react-app project )
  | backend ( your backend (express?) project )

In the your_project folder, do an npm init. Then, $ npm i concurrently so you can run your backend and the react dev server... concurrently. Modify the package.json in your_project to have something like this:

    "start": "concurrently --kill-others \"cd frontend && npm start\" \"cd backend && npm start\"",

Finally, in the package.json of your frontend folder (that's the react app), add a proxy block like this:

  "proxy": {
    "/api": {
      "target": "http://localhost:9000",
      "secure": false
    }
  }

In this example, your react app runs on whatever port (let's say 3000) and your backend is running on port 9000. Any request to http://localhost:3000/api/* will be proxied to http://localhost:9000/api/*.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment