How To Integrate Node JS With React
Before we go any deeper in this article, the first question is; why do you need to integrate node JS with React anyway? Well, I will just say this; tech enterprises such as Netflix have integrated Node JS into their systems and seen tremendous growth and improved performance. So if multinational companies are doing it, there must be a lot to gain by integrating Node JS with React. Not only can you create apps and web pages doing this but also significantly improve the functions your product has to offer. In turn, you will improve the user experience.
How to integrate Node JS with React:
After you create the backend of the app using Node JS and establish the front end user interface with React app, you can integrate the two systems using CORS.
What CORS essentially does is allows a browser to connect and transfer information to the server. Hence your front end and back end can communicate. A great feature of CORS, that we take advantage of here, is that it is easily stored on different domains.
These are the steps involved in integrating Node JS with React:
- Build your main project directory.
- Establish the back end of an app using Node JS (using the command: express-generator)
- Edit the back end to integrate Node JS with React.
- Create the front end using React (create-React-app).
- Final steps to Integrate Node JS with React using directories ( CORS and express-file-upload)
Below you will find a detailed step by step process along with the required code. But before that, let us define some terms that are used in this article:
When you use Node JS, you will need npm. This is where different node packages are stored and managed for node users. Npm stands for Node Package Manager. Users can share packages they code via npm. Hence the popularity of Node JS is heavily due to the ease created by this package manager.
Now let us begin integrating Node JS with React. Follow the below-mentioned steps:
Main project directory:
Make sure Node JS is up and running. Access your terminal and define the directory code to determine where you want your app to be stored. This is where you will keep your final files after integrating Node JS with React.
After you complete steps 2-4, this is where you will find the express app named API (back end) and the React app names CLIENT (front end). Having both your systems within the same directory makes it easier to navigate the process of integrating Node JS with React.
Start by building the back end using Node JS:
Since our ultimate goal is to integrate Node JS with React, we will use the express-generator to create the backend here. For that purpose, begin by installing the express-generator (if you don’t have it already). Use the npm module (npm nxp) to do this.
Name your express app as API. When you are done installing express-generator you will see that it comes with multiple directories. Some of these that are essential include Morgan and body-parser. You have the choice to install the rest or ignore them for the time being.
At this point, you will install the dependencies that are necessary to integrate Node JS with React. CORS and express-file-upload both support the two frameworks and give you a cross-origin resource sharing option. These dependencies allow you to upload files into the app using these two front end and back end frameworks.
After adding dependencies, you are ready to alter the code in the app.js file in the API trajectory. This is to incorporate all the packages together. Import the CORS package and express file into the code. You will then change the app.js code to tell the API to use CORS. After integrating the two new dependencies app.js code should look something like this:
Great! You are almost done setting up the back end for integrating Node JS with React. Just to be sure that you did not miss any steps, run your server to see if everything is starting smoothly. Use npm start to check the working of Node JS.
Your screen should display the words “welcome to express” now. It means you have successfully installed and prepared the back end of your app, and you are ready to move on with the process of integrating Node JS with React.
Edit back end code (Node JS):
Our next step will be to create the front end UI (user interface), but before that, you need to edit the backend port (from 3000 to 4000). So that when both the systems run on port 3000 later on, the process can continue without a hitch. The changes you will make here will be in the bin/www file. Use the code in the image below to guide yourself through these alterations:
Create the front end app using React:
To set us the front end of the app, use the command line create-command-app. Give the name CLIENT to your React app. This process is quite similar to the one you use when creating a React app without the intention of integrating Node JS with React. However, you need to make a few changes in certain steps. For instance, you will need a library to allow API calls between the front end and the back end. Here you can use the code yarn–add–Axios.
We have already covered in the previous article that React functions in the form of components. All the code is stored there and you can make changes by accessing the virtual DOM. Now you will alter the app component of src/App.js as follows:
Once again, you will check to see if the React app you created is working properly. Go to localhost:3000/ via your browser. You should see the React welcome page at this point. This means your front end is established and you are all set to integrate Node JS with React.
Integrate Node JS with React app:
For you to make a connection between the API express app and the CLIENT React app, you first have to establish a route in the express app. To do this, access the directory you created in step 1 of this guide. Open the express API folder and create a testAPI.js file in api/routes. Then insert the new route (see line 25 in the code below). Your new code in api/apps.js will be as following:
This creates a new route after which API begins to work properly in your browser.
Now open the app.js file in the CLIENT directory. Since your express app is established (CORS successfully installed in step 2) and working properly you can now fetch API. In simpler terms, this means you can now change certain parts of the HTTP pathway. This will ease the integration of Node JS with React. Use this code before beginning the render process.
The final code will look like this:
In the code above, you have inserted a command (call API ()) that will automatically access the API express app after components are attached. Line 29 of the above code enables your app to take information from API and display text on the user interface screen.
In the final step of integrating Node JS with React, run both the apps using npm start command. Open your browser and visit localhost:3000/. Here you will see the screen display two lines for you.
- One says Welcome to React
- The other saying API is working properly.
This means the integration process is complete and you now have an app with Node JS as the backend and React as the front end user interface!
You can use this link of GitHub to get the entire code.
After integrating Node JS with React, you can create fully functioning web pages and apps. CORS is installed in the back end Node JS along with the other directory called express-file-upload. Then you code port 3000 so that both the frameworks can run without crashing into each other.