

Now start up the React app by running yarn start inside the client folder. I also recommend opening up src/index.js and removing the call to registerServiceWorker() at the bottom, since it can cause some confusing caching issues (like preventing you from accessing the API endpoints in a browser after you load the React app once). get ( '/api/passwords', ( req, res ) => join ( _dirname, 'client/build' ))) // Put all API endpoints under '/api' app. Call it rando or whatever you want.Ĭonst express = require ( 'express' ) const path = require ( 'path' ) const generatePassword = require ( 'password-generator' ) const app = express () // Serve static files from the React app app. Make a parent directory to contain everything. Just a quick disclaimer: this is just meant as a demo! I don’t recommend using some random internet thing that generates passwords on the server to generate your own real passwords ) Create the Express App Every time you load the app or click Get More you’ll get 5 random paswords. (On a Mac with Homebrew, just brew install heroku).

Heroku comes with a commandline command they call a “toolbelt.” Follow the instructions here to install it.

If you don’t have one already, go here and sign up. Heroku is easy to deploy to and free to get started with. We’ll build the Express server to serve React’s static files in addition to providing an API, and then deploy it to Heroku. This article will cover how to keep them together. e.g., NGINX webserver proxies API requests to the API server, and.Put the API behind a proxy – Express and React app files sit on the same machine, but served by different servers.e.g., React app served by Amazon S3, API server running on a DigitalOcean VPS.Split them apart – Host the Express API on one machine, and the React app on another.e.g., a DigitalOcean VPS running Express on port 80.Keep them together – Express and React files sit on the same machine, and Express does double duty: it serves the React files, and it also serves API requests.Now – how do you deploy them both to a server? You’ve got a React app, and an API server written in Express or something else.
