How to Connect frontend to the backend?

Objectives

What this blog will cover:

In this blog, I’ll walk you through the process of connecting your simple React app to a simple Node/Express API that we will create.

The objective here is to give you a practical guide on how to set up and connect the front-end client and the back-end API.

Step 1: Create a React App

This process is really straightforward.

I will be using create-react-app to easily create a react app named client:

$ npx create-react-app client
$ cd client
$ npm install
$ npm start

The commands above are simple, the first line uses node to create the React application, second moves us into the newly created directory called client which is our app. Finally, we start running the application to run it on our localhost (default localhost:3000).

Step 2: Create a Express App

I will be using the ExpressJS to quickly create an application skeleton and name it exp:

$ mkdir exp
$ cd exp

create a directory to hold your application, and make that your working directory.

$ npm init

This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:

$ npm install express --save

Now install Express in the exp directory and save it in the dependencies list.

Step 3: Setting up simple backend server

const express = require('express') 
const app = express() 
const port = 8080 

app.get('/', (req, res) => {   res.send('Hello World!') }) 

app.listen(port, () => {   
console.log(`Example app listening at http://localhost:${port}`) })

After completing step 3 within the integrated terminal we can run node index.js to deploy our backend server locally.

Once you have a simple backend server we can go back to the React application and edit the package.json file. In that file, we need to add “proxy”: http://localhost:8080″

The two applications can now talk to each other. Depending on what you want to do we can specify get and post functions in our backend and link it to our front end using Axios.

My Top VS Code Extensions for Web Development

Visual Studio Code is the most popular text editor out there and for good reason to. I enjoy editing on VS Code because of how customizable it is, when first installed it is a very barebones environment but with some modifications to the theme, settings.json and the addition of extensions users can turn a simple text editor into their perfect IDE for their needs. So here are my top extensions:

  1. GitLens – This extension supercharges the Git capabilities built into Visual Studio Code. It helps you to visualize code authorship at a glance via Git blame annotations and code lens, seamlessly navigate and explore Git repositories, gain valuable insights via powerful comparison commands, and so much more.

2. Bracket Pair Colorizer – This extension allows matching brackets to be identified with colours. The user can define which tokens to match, and which colours to use. Users are also able to modify the settings.json with “bracket-pair-colorizer-2.colors” to change the bracket colours to their own custom ones rather than the default.

3. emojisense: – This extension lets users insert emoji’s into their code with a simple short cut of CTRL-i. No need to google for emoji’s or find hex codes online to put into your strings you have all the emoj’s at your finger tips.

4. ErrorLens – This highlights any errors or warnings users have with a short detailed message. It even combines with other extensions to provide a consistent look to your editor, for example combined with CodeSpellChecker it highlights any spelling errors in your code so they’re easily noticeable rather than a dull little warning in your status bar.

5. PowerLevel10k (Terminal Theme): A Zsh theme (github.com) – This is not so much of an extension but it is a terminal theme that uses ZSH. However this theme is not just aesthetically pleasing to look at, it is very practical too. Again it is a theme that is fully customizable by users with segments. For example, within the segments, you can add the status of your battery, your OS, time, public IP, azure account name and even the status of your VPN.