{"id":13941255,"url":"https://github.com/fullstackreact/food-lookup-demo","last_synced_at":"2025-09-21T00:09:01.996Z","repository":{"id":41186790,"uuid":"64346297","full_name":"fullstackreact/food-lookup-demo","owner":"fullstackreact","description":"A demonstration of using `create-react-app` with a server","archived":false,"fork":false,"pushed_at":"2018-10-09T14:43:51.000Z","size":5373,"stargazers_count":1190,"open_issues_count":19,"forks_count":373,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-05-23T13:23:02.788Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fullstackreact.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-27T22:38:52.000Z","updated_at":"2025-05-02T01:58:51.000Z","dependencies_parsed_at":"2022-08-25T21:00:33.078Z","dependency_job_id":null,"html_url":"https://github.com/fullstackreact/food-lookup-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fullstackreact/food-lookup-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fullstackreact%2Ffood-lookup-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fullstackreact%2Ffood-lookup-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fullstackreact%2Ffood-lookup-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fullstackreact%2Ffood-lookup-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fullstackreact","download_url":"https://codeload.github.com/fullstackreact/food-lookup-demo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fullstackreact%2Ffood-lookup-demo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276175329,"owners_count":25597801,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-20T02:00:10.207Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-08T02:01:14.965Z","updated_at":"2025-09-21T00:09:01.979Z","avatar_url":"https://github.com/fullstackreact.png","language":"JavaScript","readme":"# `create-react-app` with a server example\n\n[![TravisCI](https://travis-ci.org/fullstackreact/food-lookup-demo.svg?branch=master)](https://travis-ci.org/fullstackreact/food-lookup-demo)\n[![Dolphins](https://cdn.rawgit.com/fullstackreact/food-lookup-demo/master/dolphins-badge.svg)](https://www.fullstackreact.com)\n\nThis project demonstrates using the setup generated by `create-react-app` alongside a Node Express API server.\n\n![](./usage-demo.gif)\n\n### Detailed blog post\n\nWe have a [detailed blog post](https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/) that explains this repository.\n\n### Rails\n\nCheck out the [Rails version](https://github.com/fullstackreact/food-lookup-demo-rails) if that's your preferred API server platform.\n\n## Running locally\n\n```\ngit clone https://github.com/fullstackreact/food-lookup-demo.git\ncd food-lookup-demo\nnpm i\n\ncd client\nnpm i\n\ncd ..\nnpm start\n```\n\n## Overview\n\n`create-react-app` configures a Webpack development server to run on `localhost:3000`. This development server will bundle all static assets located under `client/src/`. All requests to `localhost:3000` will serve `client/index.html` which will include Webpack's `bundle.js`.\n\nTo prevent any issues with CORS, the user's browser will communicate exclusively with the Webpack development server.\n\nInside `Client.js`, we use Fetch to make a request to the API:\n\n```js\n// Inside Client.js\nreturn fetch(`/api/food?q=${query}`, {\n  // ...\n})\n```\n\nThis request is made to `localhost:3000`, the Webpack dev server. Webpack will infer that this request is actually intended for our API server. We specify in `package.json` that we would like Webpack to proxy API requests to `localhost:3001`:\n\n```js\n// Inside client/package.json\n\"proxy\": \"http://localhost:3001/\",\n```\n\nThis handy features is provided for us by `create-react-app`.\n\nTherefore, the user's browser makes a request to Webpack at `localhost:3000` which then proxies the request to our API server at `localhost:3001`:\n\n![](./flow-diagram.png)\n\nThis setup provides two advantages:\n\n1. If the user's browser tried to request `localhost:3001` directly, we'd run into issues with CORS.\n2. The API URL in development matches that in production. You don't have to do something like this:\n\n```js\n// Example API base URL determination in Client.js\nconst apiBaseUrl = process.env.NODE_ENV === 'development' ? 'localhost:3001' : '/'\n```\n\nThis setup uses [concurrently](https://github.com/kimmobrunfeldt/concurrently) for process management. Executing `npm start` instructs `concurrently` to boot both the Webpack dev server and the API server.\n\n## Deploying\n\n### Background\n\nThe app is ready to be deployed to Heroku.\n\nIn production, Heroku will use `Procfile` which boots just the server:\n\n```\nweb: npm run server\n```\n\nInside `server.js`, we tell Node/Express we'd like it to serve static assets in production:\n\n```\nif (process.env.NODE_ENV === 'production') {\n  app.use(express.static('client/build'));\n}\n```\n\nYou just need to have Webpack produce a static bundle of the React app (below).\n\n### Steps\n\nWe assume basic knowledge of Heroku.\n\n**0. Setup your Heroku account and Heroku CLI**\n\nFor installing the CLI tool, see [this article](https://devcenter.heroku.com/articles/heroku-command-line).\n\n**1. Build the React app**\n\nRunning `npm run build` creates the static bundle which we can then use any HTTP server to serve:\n\n```\ncd client/\nnpm run build\n```\n\n**2. Commit the `client/build` folder to source control**\n\nFrom the root of the project:\n\n```\ngit add client/build\ngit commit -m 'Adding `build` to source control'\n```\n\n**3. Create the Heroku app**\n\n```\nheroku apps:create food-lookup-demo\n```\n\n**4. Push to Heroku**\n\n```\ngit push heroku master\n```\n\nHeroku will give you a link at which to view your live app.\n","funding_links":[],"categories":["Integrating with an API Backend","JavaScript"],"sub_categories":["Node"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffullstackreact%2Ffood-lookup-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffullstackreact%2Ffood-lookup-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffullstackreact%2Ffood-lookup-demo/lists"}