This is a very common question among newer React developers, and one question I had when I was starting out with React and Node.js. In this short example I will show you how to make create-react-app
work with Node.js and Express Back-end.
In my case, 'create-react-app' was not installed. To install run this command. Npm install -g create-react-app Once installation successful, try running. Npx create-react-app hello-world If this works for you. Great else comment what worked for you. You can test this by seeing if adding the ignore existing flag makes it work: npx -ignore-existing create-react-app newapp. ( source, but also mentioned here ) Although npx -ignore-existing create-react-app newapp worked (when npx create-react-app newapp did not), I figured that removing old node files (see other chapter), removing the. Create-react-app takes care of all the configuration, but it does come at a price. We can't modify configurations to suit our needs, because that is the job of create-react-app. But for adding our rust module and WebAssembly into our build, we need to update webpack config.
create-react-app
Create a project using create-react-app
.
Create a /client
directory under example-create-react-app-express
directory and move all the React boilerplate code created by create-react-app
to this new client directory.
The Node Express Server
Create a package.json
file inside the root directory (example-create-react-app-express
) and copy the following contents:
Notice I am using concurrently
to run the React app and Server at the same time. The –kill-others-on-fail
flag will kill other processes if one exits with a non zero status code.
Install nodemon
globally and the server dependencies:
Create a server.js
file and copy the following contents:
This is a simple Express server that will run on port 5000 and have two API routes: GET
- /api/hello
, and POST
-/api/world
.
At this point you can run the Express server with the following command (still inside the root directory):
Create React App 17
Now navigate to http://localhost:5000/api/hello
, and you will get the following:
We will test the POST
route once we build the React app.
The React App
Now switch over to the client
directory where our React app lives.
Add the following line to the package.json
file created by create-react-app
.
The key to using an Express back-end server with a project created with create-react-app
is to use a proxy. This tells the Web-pack development server to proxy our API requests to our API server, given that our Express server is running on localhost:5000
.
Now modify ./client/src/App.js
to call our Express API Back-end, changes are in bold.
We create callApi
method to interact with our GET
Express API route, then we call this method in componentDidMount
and finally set the state to the API response, which will be Hello From Express.
Notice we didn’t use a fully qualified URL http://localhost:5000/api/hello
to call our API, even though our React app runs on a different port (3000). This is because of the proxy
line we added to the package.json
file earlier.
We have a form with a single input. When submitted calls handleSubmit
, which in turn calls our POST
Express API route then saves the response to state and displays a message to the user: I received your POST request. This is what you sent me: [message from input].
Now open ./client/src/App.css
and modify .App-header
class as follows (changes in bold)
Running the App
If you still have the server running, go ahead and stop it by pressing Ctrl+C in your terminal.
From the project root directory run the following:
This will launch the React app and run the server at the same time.
Now navigate to http://localhost:3000
and you will hit the React app displaying the message coming from our GET
Express route. Nice ?!
Now, type something in the input field and submit the form, you will see the response from the POST
Express route displayed right below the input field.
Finally take a look at at your terminal, you will see the message we sent from the client, that is because we call console.log
on the request body in the POST
Express route.
Production Deployment to Heroku
Open server.js
and replace with the following contents:
Create React App Eject
Open ./package.json
and add the following to the scripts
entry
Heroku will run the start
script by default and this will serve our app. Then we want to instruct Heroku to build our client app, we do so with heroku-postbuild
script.
Now, head over to Heroku and log in (or open an account if you don’t have one).
Create a new app and give it a name
Click on the Deploy tab and follow the deploy instructions (which I think they are pretty self-explanatory, no point on replicating them here ?)
And that is it, you can open your app by clicking on the Open app button at the top right corner within the Heroku dashboard for your app.
Visit the deployed app for this tutorial: https://cra-express.herokuapp.com/
Other Deployment Options
I write about other deployments options here:
- Heoku (more in-depth explanation)
Project Structure
This will be the final project structure.
Get the full code on the GitHub repository.
Thank you for reading and I hope you enjoyed it. Any question, suggestions let me know in the comments below!
You can follow me on Twitter, GitHub, Medium, LinkedIn or all of them.
This post was originally posted on my personal blog website.
Update 8/25/19: I have been building a prayer web app called 'My Quiet Time - A Prayer Journal'. If you would like to stay in the loop please sign up through the following link: http://b.link/mqt
The app will be released before the end of the year, I have big plans for this app. To see some mockup screenshots follow the following link: http://pc.cd/Lpy7
My DMs on Twitter are open if you have any questions regarding the app ?
If you can't sign in to FaceTime
- Make sure that you can sign in at appleid.apple.com. Learn what to do if you forgot your Apple ID or forgot your Apple ID password.
- Choose Apple () menu > System Preferences, then click Date & Time and select “Set date and time automatically”. Click Time Zone and make sure that the settings are correct.
- Make sure that you've installed the latest software updates.
If your camera or microphone doesn’t work
One of these solutions might help:
- Quit FaceTime, then open FaceTime again.
- Restart your Mac (Apple menu > Restart).
- Make sure that FaceTime isn't open, then see if your camera works in another application, such as Photo Booth. If it doesn't, you know that the issue isn't related to FaceTime.
- If you have multiple cameras or microphones connected to your Mac, check the Video menu in the FaceTime menu bar to make sure that you have the correct camera and microphone selected.
If you need help with your connection or call quality
A slow or busy Wi-Fi network might cause issues with your FaceTime call. If you experience connection alerts or low-quality audio or video:
Create-react-app Not Found Mac
- Make sure that you and your recipient have a fast Wi-Fi or cellular connection. FaceTime calls over Wi-Fi require a broadband connection.
- Check for issues that might be slowing down your connection, such as someone streaming content over your network.
- If you have questions about your Internet speed, contact your Internet service provider.
FaceTime is not available in all countries or regions.