How to build an application using ReactJS and Node back end server API?

0
92
ReactJS + nodejs

Overview

In today’s world of development, the importance of React as a JavaScript library is well-known. Likewise, its combination with Node.js on the server-side can be used to yield a powerful combination. In this article, we shall primarily look at how one can build an application, making use of ReactJS on the front-end and Node.js on the backend.

What is ReactJS?

ReactJS is a JavaScript library that is mainly used for the development of front end-user interfaces. At present, it is maintained by Facebook, along with a number of individual developers and companies. The primary reason why React is used is that it can be used to fetch rapidly changing data that also needs to be recorded.

So, to just understand what React is, let us know the following –
• It is one of the most popular JavaScript libraries
• Unlike Angular, which is opinionated, React is not a framework of any sorts
• It is an open-source project, and as stated earlier, it was created by Facebook
• It is used for the building of user interfaces (UI) on the front-end
• In an MVC (Model-View-Controller) setup, React comprises of the View portion.

One of the primary aspects of React is that one can create components, which are mainly custom HTML elements that are reusable for a quick and efficient build-up of user interfaces.

React can also streamline how data is stored and handled, making use of state and props.

Pros and cons of ReactJS.

Just like using any other JavaScript library, React too comes with its own set of advantages and disadvantages.

Pros –
• React is very easy to learn and use. There is a good amount of documentation, tutorials and resources that help in learning. Also, a developer with any prior experience on JavaScript can easily grasp React and start working with it quickly.
• The use of React makes it easier for the dynamic development of Web applications. Using HTML strings to do so was a pretty complex task. However, the use fo React has simplified the process. Making use of JavaScript Extension, a syntax that allows HTML quotes and HTML tag syntax to components, it offers more functionality.
• Components can be reused when you use React. There can be multiple components and each possesses its logic and controls. Also, every component works to output a small and reusable piece of HTML code that can be reused wherever required. Thus development and maintenance of applications are pretty easy.
• React also leads to enhancement in performance. This is largely attributed to the use of virtual DOM. DOM, as we know, is a cross-platform programming API that deals with HTML, ZML, and XHTML. The major problem faced by developers was when the DOM was updated it slowed down performance. The React Virtual DOM exists in the memory and happens to be the representation of the browser’s DOM.
• React also boasts of handy tools that make the tasks of developers very easy. Designed as Chrome and Firefox dev extension, they allow inspection of React hierarchies in the virtual DOM.
• React is SEO friendly unlike that of traditional JavaScript frameworks. Search engines are known to face problems with reading JavaScript-heavy applications. However, React overcomes such a problem.

Cons
• High paced development happens to be both an advantage and a disadvantage. It is disadvantageous because the rapidly changing environment makes it difficult for developers to keep up with the continuous set of updates.
• Another major con of React is the lack of proper documentation. Since React is a rapidly updating, there is not much time to make proper documentation.
• React only takes care of the View part of the application development process. Hence, one will need other technologies to develop the other parts.
• JSX, which is part of React, employs syntax that uses HTML mixed with JavaScript. However, many in the development community consider this to be a barrier and is quite complex when it comes to the learning curve.

What is NodeJS?

Node.js

NodeJS, or rather, Node.js is a JavaScript runtime environment that can be used across the platform. It is a complete package that includes everything that is required to execute a program in JavaScript.

The existence of NodeJS happened when the developers of JavaScript extended its functionality from something that only ran on the browser to one that could be run standalone on a machine. NodeJS is presently used to run server-side scripts and brings together web application development around one programming language.

What is JSX?

JSK, which is known as JavaScript Extension, is basically a syntax extension to JavaScript. The syntax is neither HTML nor JavaScript but rather a mix of both these languages.
It is recommended that JSK is used when using React to describe what the UI must look like. JSX also comes with the full power of JavaScript.

Our assumption

In order to work with React, one will need to have some sort of familiarity with JavaScript. Also, if you happen to come from a different programming language, you have to know concepts such as objects, functions, arrays, and also classes.

Setup and Installation

In this section, we will discuss the setup of a full-stack environment. The front end part would be ReactJS and the back end would be NodeJS.

Installing ReactJS

First, we will check the simple steps to install the ReactJS front end tool. Open your working directory and run the following commands step by step. You can make any directory as your working directory. Please remember, the working directory should be a separate workspace where you install your environment and run your application.
1. mkdir eduonix_react_example
2. npm i -g create-react-app
3. cd eduonix_react_example
4. create-react-app eduonix_client
5. cd client
6. npm start

Following is the sample package.json created from the above configuration. In this example, we have used the listing port as 3001. You can use any other open port and modify the package.json file accordingly.

Listing 1: Sample package.json file

</p>

<pre class="lang:js decode:true">{
“name”: “eduonix_client”,
“version”: “0.1.0”,
“private”: true,
“proxy”: “http://localhost:3001/“,
“dependencies”: {
“react”: “16.8.6”,
“react-dom”: “16.8.6”,
“react-scripts”: “3.0.1”
},
“scripts”: {
“start”: “react-scripts start”,
“build”: “react-scripts build”,
“test”: “react-scripts test?-?env=jsdom”,
“eject”: “react-scripts eject”
}
}</pre>
<p align="justify">

Now, our React setup is complete. We can check it by opening the localhost at port 3000.

Installing NodeJS

Async Operations

Now, we will check step by step process to set up the NodeJS server. Here we will be using the Express framework. Please follow the steps below to install and run the NodeJS server.
1. cd eduonix_react_example
2. mkdir eduonix_server
3. npm install express-generator -g
4. express - - view=ejs server
5. cd eduonix_server
6. npm install
7. Go to server folder (here eduonix_server)
8. Open package.json file

The package.json will be as shown below. Here, we need to do a minor modification on the port number. We need to use some other port and not 3000.Because, our front end component React is running on port 3000. Let’s make it 3001 or any other. Now, the Node.JS set up is ready for connecting.

Listing 2: Sample package.json file (for NodeJS server)

[/code]

[/code]

Connecting ReactJS and NodeJS

In this section, we will connect the front end with the back-end server. Please follow the steps below to connect ReactJS with the Node server.
1. Go to eduonix_react_example
2. npm init –y
3. Now a separate package.json file will be created outside server and client folder

The package.json file will look like this.

Listing 3: Sample package.json file (for connecting node server)

</p>

<pre class="lang:js decode:true">{
“name”: “connect_node”,
“version”: “1.0.0”,
“description”: “”,
“main”: “index.js”,
“scripts”: {
“client”: “cd eduonix_client &amp;&amp; npm start”,
“server”: “cd eduonix_server &amp;&amp; npm start”,
“start”: “concurrently?-?kill-others \”npm run eduonix_server\" \"npm run eduonix_client\""
},
“keywords”: [],
“author”: “Eduonix”,
“license”: “ISC”,
“devDependencies”: {
“concurrently”: “³.5.1”
}
}</pre>
<p align="justify">

Now our ReactJS and NodeJS are up and running on port 3000 and 3001. So, you can connect the front end and back end to create various applications.

Conclusion

React and NodeJS is definitely a powerful combination, and that is exactly what has been demonstrated in this article. It gives you a basic idea about what React and NodeJS is and takes you through a sample application to help you get familiar with the concepts. You can also learn more about connecting a React App with Nodejs and how to become a React developer.

To learn about React, try the “React for Professionals: The App Building Guide”. The course comes with over 4 hours of video that covers 4 major sections. The topics involve Node.js, NPM, Mapbox, GeoJSon, APIs, React life-cycles and much more.

And if you are a beginner in React, you can try out the “React for Absolute Beginners” online course. This course comes with 5 hours of video that covers 6 major topics, such as an introduction to React, server-setup, HTML-setup, and much more.

LEAVE A REPLY

Please enter your comment!
Please enter your name here