DEV Community

Katarina Harbuzava for Flatlogic

Posted on • Updated on • Originally published at flatlogic.com

Using React With Express

React is an open source frontend library. Not just for single-page applications but it is employed for complex and massive web interfaces as well. This library powers a plethora of websites in the current times.

After the launch of this library, the world has also witnessed a massive growth in the utilization of robust and lightweight JavaScript libraries. People want to opt for more and more dynamic website pages while developers require flexible environments. This is the reason for the rising popularity of ReactJS. Let us dig deep for learning why you must hire ReactJS development services to build a blog with React.

  • ReactJS is well known for its simplicity. Any developer, who has good know-how about JS functions, can easily start using ReactJS. Developers will be able to define interfaces with JSX that are syntax like HTML. Due to this, the production of CSS & HTML occurs. React’s API is robust and anyone can simply get started by learning some basic functions.

  • Development is no more complex with the emergence of ReactJS. The library offers the exact tools for building projects with reusable components. By this the interface elements that are already built can be utilized anywhere in the project by simply calling from various other components. Write a function & reuse it anywhere later on.

Benefits of Creating an App With React.js

Speed – Creation of React blog CMS is always beneficial as it permits developers for utilizing individual portions of their particular application on both the server and client side. This boosts the pace of the entire development process.

Performance – The core of ReactJS provides a virtual server side rendering & DOM program that enables the fast running of complex applications as well.

Steps to Building an App With React.js

  • Express Setup – To create a blog using React start by installing the express generator with this command:
npm install express-generator
Enter fullscreen mode Exit fullscreen mode

Run the express command in the Server directory. By this you can get hands on a default express application but do not utilize the default configuration as you need to modify it first. After deleting routes, views and public folders, you can start with 3 files that are package.json, www file in bin directory and app.js.

Add 3 extra libraries such as:

  1. Cors – this will help to maintain communication between the Express server & React App.
  2. Helmet – Making HTTP requests extra secure, this library will keep on updating HTTP headers.
  3. Pg – This major library will be utilized for communicating with psql databases. Communication with databases will never be possible without this.

Use this command for installing the libraries

npm install pg helmet cors
Enter fullscreen mode Exit fullscreen mode

Be sure that your server is functioning correctly by witnessing certain commands while you aim to create your blog in ReactJS.

  • Connecting to client side

A React Native blog can only be built when you connect the client side application to the server. Simply go to package.json file in the Client directory & enter the command that says:

proxy: http://localhost:5000"
Enter fullscreen mode Exit fullscreen mode

Clients can now easily communicate with a server via a proxy.

Now return to server side & set express routing. Craft a fresh file named routes.js in the major folder in the Server directory. This file will retain all express routes that will allow you to send data to client side applications. For now set a simple route.

var express = require('express')
var createError = require('http-errors');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var app = express();
var router = express.Router()
router.get('/api/hello', (req, res) => {
  res.json('hello world')
})
app.use('/', router);
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Listening on port ${PORT}`);
});
module.exports = app;
Enter fullscreen mode Exit fullscreen mode

The command for client side code in home.js component is:

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

const Home = props => {
    useEffect(() => {
    axios.get('/api/hello')
        .then(res => setState(res.data))
    }, [])

    const [state, setState] = useState('')

  return(
    <div>
    Home
    <p>{state}</p>
    </div>
 )
};
export default Home;
Enter fullscreen mode Exit fullscreen mode

Lastly, to build a blog with react tutorial, you or if you hire ReactJS developer must make an axios get request to the running express server. If it functions properly, you will be witnessing “hello world” that will be rendered to screen.

When it works perfectly, you must know that a React Node fullstack application has been built.

Final Say

You can create a React app blog by going through the above mentioned steps and commands. But in case you are a novice and unfamiliar about how to write a blog in React, it is best to hire a ReactJS developer. What a professional can do, can hardly be achieved by someone who is still in the learning process. So connect with us today for working with our sound and experienced ReactJS developers.


You might also like these articles:
Top Javascript Maps API and Libraries
17+ Articles of July to Learn Javascript
20+ React Developer Tools to Increase Your Programming Productivity


Originally published at flatlogic.com — React, Angular, Vue, Bootstrap & React Native templates and themes.

Text source: Using React With Express

Top comments (0)