The combination of frontend React app development and Node backend is a solid combination to create any application. This guide aims to make it as easy as possible to create a full-fledged project with React. We’ll look at how to build a complete project from scratch using React and Node and deploy it to the web.

Want to build and deploy your own React App Development and Node applications? Check out my series of courses that will show you how to create a complete React project yourself.

Things You will require for React App Development

  1. Make sure you have Node and NPM installed for React app development. Both can be downloaded from nodejs.org (NPM is included in the Node installation).
  2. Use your favorite code editor. I personally use and recommend VSCode, which can be downloaded from code.visualstudio.com.
  3. Make sure you have Git installed on your computer for React app development. This is required to deploy the application on Heroku and can be downloaded at git-scm.com.
  4. If you have an account on heroku.com, you plan to use Heroku to distribute your web app completely free.

Process React App Development

Step 1: Create a backend node (Express)

For React App Development create a folder first. Then drag this folder into the code editor.

To create a Node project, run the following command in the terminal

npm init -y

This will create a package.json file that keeps track of all the application scripts and manages all the dependencies of the Node application. The server code is stored in a folder with the same name as the server. Let’s create this folder.

In this folder, place the index.js file, which is used to start the server. If the environment variable PORT is not specified, Express will create a simple web server running on port 3001 (Heroku sets this value when deploying the application).

// server/index.js

const express = require(“express”).

const PORT = process.env.PORT || 3001;

const app = express();

app.listen(PORT, () => {)

console.log(`Server listens to ${PORT}.

});

Then install express as a dependency in the terminal to use it.

npm i express

Then, in package.json, create a script that starts the web server when run with npm start.

// server/package.json

“Script. {

“start”: “node server/index.js”

},

Finally, run npm start in the terminal to start the application and you should see it running on port 3001.

npm start.

> node server/index.js

Step 2: Create API Endpoint 

For React App Development We want to use Node and the Express server as APIs to provide data to React applications, change data, and perform other actions that only a server can perform. In our case, we want the React application to send a “Hello from the server!” message in a JSON object.

The code below creates a route/api endpoint.

// server/index.js

app.get(“/api”, (req, res) => {)

res.json({ message: “Hello from server!”)

});

app.listen(PORT, () => {).

console.log(`server is listening to ${PORT}`).

});

Note: This function should be placed on top of the listening function of the application.

Now that you have made changes to the node code, you need to restart the server. To do this, exit the startup script by pressing Command/Ctrl+C in the terminal, then run npm start again to restart the startup script. To test this, you can visit http://localhost:3001/api in your browser and see what’s there.

Step 3: Create the React Frontend

Now that the backend has been created, we can now turn our attention to the frontend.

You now have a React app with all dependencies installed. The only thing that needs to be changed is to add a property called proxy in the package.json file.

This will allow you to make requests to the node server without having to specify the running origin (http://localhost:3001) for each network request.

The React application can then be started by running the same startup script as the Node server. First, check the cd of the newly created client.

Step 4: Make an HTTP Request from React to Node

Now that we have a working React application development, we want to use it to interact with our API.

Let’s see how to get data from the /API endpoint we created earlier. To do this, we can go to the App.js component in the src folder and use useEffect to make an HTTP request.

We will use the Fetch API to make a simple GET request to our backend and return our data in JSON format. Once we have the data returned, we will get the message attribute (grab the greeting we sent from the server) and put it in a state variable called data.

This will allow us to display that message on our page, if we have one. We use a condition in JSX that says to display the text “Loading…” if our data is not already there.

// client/src/App.js

import React from “react”;

import logo from “./logo.svg”;

Enter “./App.css”.

function App() {

const [data, setData] = React.useState(null);

React.useEffect(() => {

fetch (“/api”)

.then((res) => res.json())

.then((data) => setData(data.message)).

}, []);

return (

<div className=”App”>

<header className=”App-header”>

<img src={logo} className=”App-logo” alt=”logo” />

<p>{!data ? “Loading…” : data}</p>

</header>

</div>

);

}

Step 5: After Decompressing the Heroku Network.

For React App Development Last but not least, the app is also available online. We confirmed that the Git repository has been automatically initialized by creating create-react-app in Krai-Anentfold.

This is necessary to ensure the smooth running of the year’s work. Once deployed, both the Node backend and the React frontend are deployed to the same domain (i.e. mycoolapp.herokuapp.com). Rikuest’s Nodo API is a tool for manipulating data; it is necessary to use Aspire React to render it (for example, to move a page from Aspire).

This code is located in the server/index.js file where the following code is attached.

// servidor/index.js

const path = require (‘path’);

const express = require (‘express’);

// “Constructing Appel’s Reaction” (Nó ligado, Construindo a Reaffel).

app.use ( express. static ‘./cliente/construção’).

// Make a GET request using the /api route.

app.get(“/api”, (req, res) => {)

res.json({ mensagem: “Olá do servidor!” }).

});

// É um processamento do GET request de outros all para o React app.

app. get(‘*’, (req, res) => {)

res.sendFile(path.resolve(__dirname, ‘./cliente/construção”, ‘index.html’)).

});

The Este code first allows Node to access the static files of the configured React project using the express. static function.

It also uses an api route to handle GETlixto, which React Applique responds to.

This is a great approach to Reage and Node applications.

It is also possible to attach the servidor’s package.json file to VIRTUALSCOMPT and instruct the Node application method for this Reação application.

// servidor/pacote.json

},”script”. {

“start”: “node server/index.js”.

“build”: “cd client && npm install && npm run build”

},

It is also recommended to specify a field called “engine” and to use the version of Node to build the project. It is used in Doppelmayr.

You can get the Nó version by running node -v and typing the result “engine” (e. g 14.15.4).

// servidor/pacote.json

“motor”. {

“nó”: “sua inversão de nó”.

}

After this, the copy for Heroku has been completed and Heroku.com has been acquired. We would like to inform you that we selected the “New” > “Create new application” option and we were able to save our data in a new application

Conclusion

Following these 5 steps will help you develop a React app development with node Backened. This means the app will be more efficient.