02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous Video
Chapter is concluded.Next is ExpressJS.

Going Back to React

Going Back to React

In this tutorial, we are going back to React after exploring the basics of Node.js. Understanding how Node.js and React integrate together is essential for creating a complete full-stack development experience. This tutorial, originally created in 2016, includes critical updates to ensure you can use the latest best practices.

Step 1: Revisiting the Backend

Now that we have gained some experience with Node.js and setting up a basic server, it’s time to see how we can integrate it with React. Node.js serves as the backend server that handles requests, processes data, and communicates with databases. This backend can then provide the necessary data to a React frontend.

Example: Setting Up an API Endpoint

In Node.js, you can create an API endpoint that sends data to your React application. Consider the following code snippet that creates a basic API endpoint:

const express = require('express');
const app = express();
const port = 5000;

app.get('/api/data', (req, res) => {
  res.json({ message: 'Hello from the backend!' });
});

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

In this example, we are using Express, a popular Node.js framework, to create an API endpoint /api/data. This endpoint will provide data that our React application can use.

Update (2024): Modern React applications often use REST APIs or GraphQL to communicate with the backend. Consider using GraphQL if you need more flexibility in querying data, as it can optimize performance for complex front-end applications.

Step 2: Fetching Data in React

Now that we have an endpoint set up in Node.js, let’s switch to the React side and see how to fetch data from the backend.

Example: Fetching Data from an API

In your React component, you can use the fetch API or axios to make a request to the backend:

import React, { useEffect, useState } from 'react';
import axios from 'axios';

function App() {
  const [message, setMessage] = useState('');

  useEffect(() => {
    axios.get('http://localhost:5000/api/data')
      .then(response => {
        setMessage(response.data.message);
      })
      .catch(error => {
        console.error('Error fetching data:', error);
      });
  }, []);

  return (
    <div>
      <h1>{message}</h1>
    </div>
  );
}

export default App;

In this example, we are using axios to make a GET request to our Node.js API endpoint. The response from the backend is then set in the state variable message and displayed in the React component.

Critical Update: React now supports React Hooks, such as useEffect and useState, which were not available in 2016. Hooks allow for better state and side-effect management in functional components, making the code more concise and easier to maintain.

Step 3: Understanding the Full-Stack Flow

The flow between the Node.js backend and the React frontend is as follows:

  1. Backend Setup: Node.js serves as the backend server, and we use Express to set up API endpoints.
  2. API Endpoint: The backend creates an endpoint, such as /api/data, that provides data in JSON format.
  3. Frontend Fetch: The React frontend makes a request to the endpoint using axios or the fetch API.
  4. Displaying Data: The response from the backend is used to update the state in React, which then displays the data in the UI.

This integration allows developers to create dynamic web applications that leverage the power of both a backend server and a modern frontend framework.

Conclusion

In this tutorial, we went back to React after working with Node.js. We explored how to set up a basic backend server with an API endpoint and how to fetch that data in a React application. Understanding the interaction between React and Node.js is a crucial part of becoming a full-stack developer, enabling you to build scalable, data-driven web applications.

Next Steps: Now that you understand the basics of integrating React with a Node.js backend, consider learning more about authentication, working with databases, or deploying a full-stack application to a cloud provider like AWS, Azure, or Heroku.

Video Transcript

00:00Now that we are running a server, it's time for us
00:03to go back to React.
00:04And in that react,
00:05because we have a local server, we don't need
00:08that local browser changes.
00:10That really opened up our browser to security issues
00:13where external sites can look around into our file system,
00:16which we really don't want that.
00:18So we're gonna undo that.
00:19Make sure that you do,
00:20because now that you have a server forever onwards,
00:23whenever you're doing web development,
00:24you're gonna use a server,
00:25even if it's the most basic server,
00:27even if you're only doing client side things.
00:29So let's see it in action.
00:32Alrighty, so we're turning back time
00:34and going back to a basically React one, our first section
00:38where we created our React screen.
00:41Now what I did, and you should do that as well,
00:42just click on pause for a second
00:44and literally what I'm doing is,
00:46let me just find the right folder.
00:49Here we go. I just grabbed the files, basically from here,
00:52copied all the files, just changed the name from index
00:55to index two, and we have your index two.
00:57And all the rest of the files have just
00:59imported, brought them in.
01:00So that way our file is right here at React Sample.
01:04I wanna show you how to, first of all, what, what, now
01:06that we know how to create servers, we could go ahead
01:08and enter your about config.
01:11If you're inside of uh, Firefox, find that
01:14strict origin policy
01:16and just double click on it to undo that configuration,
01:19that let things on the web access our local folders.
01:22'cause we really don't need it anymore now that we have
01:25full servers on our capability list.
01:29So, uh, once you've done that, let's just go ahead
01:30and just show you that everything will continue working.
01:33If I go into my terminal
01:35and I'm just gonna run that HTT P server,
01:37local host IP 3000, uh, more accurately, um,
01:41port 3000, and I go back into my browser
01:44and this time I'm just gonna go into my local host instead
01:46of approaching the file directly from now on,
01:49whenever you're gonna work with with your files, you wanna
01:52approach them as a server in the browser
01:54so you'll have a much more realistic environment.
01:58And there we go. And now we have our React js loading up
02:01just directly through the server so we don't have
02:04to worry about local file permissions
02:06and all that stuff that we have
02:07to worry about in our first section.
02:11And that's it with that in the next chapter.
02:13The whole section is gonna be dedicated
02:15to the most common technology used as the core server
02:20framework in no js.
02:22And that technology is called express js,
02:25which will be the dedicated time for the full chapter,
02:28first full section of the next section.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!