Open for hireContact me
Last updated:
2 min read
Different ways to connect react frontend and node backend
Rakesh Potnuru
Author
0%
SHARE

Different ways to connect react frontend and node backend

There are different ways to connect react frontend and NodeJS backend. In this blog, I am going to tell you three ways how you can connect the backend and frontend. These are the ways most developers prefer.

Prerequisites

  • React and NodeJS

Let's get started

1. Single server

Single server

The first way is to have a single server that serves both Node API and React SPA under the same domain. Here data is still exchanged through JSON. As you can see in the above picture, all the routes which do not start with /api will be handled by React SPA. This is a simple way and you don't need to worry about those CORS errors🥶.

Here's how you can do it-

  • Copy build folder files from react app and paste them in public folder of NodeJS server.
  • Now server the static index.html which in the public folder
1app.use(express.static(path.join('public'))); 2app.use((req,res) => { 3 res.sendFile(path.resolve(__dirname, 'public', 'index.html')); 4});

Pros

  • Single server.
  • No more CORS errors 😅
  • Ideal for small applications.

Cons

  • As both frontend and backend will be handled by the same server, you may face performance issues.

2. Two separated servers

Two separated servers Here we need two separate servers. One server serves static React SPA and another server serves Node API. Data will be exchanged through JSON.

Pros

  • As we use two different servers for the backend and frontend, we get better performance.
  • Ideal for bigger applications.

Cons

  • Have to maintain two different servers.

3. Template engines

The third way and the least preferred way is server-side rendering with template engines like ejs, handlebars, pugjs etc... Here we don't create any REST API.

We render different HTML pages for different HTTP requests and react will pre-render some parts of the page.

This is not the preferred way to connect React and Node because we don't get the power of reactive user experience.


So, what other ways do you know and what is your preferred way? Comment below 👇


I hope you learned something new!


LEAVE A COMMENT OR START A DISCUSSION

MORE ARTICLES

Introducing Publish Studio: Power Digital Content Creation

    3 min read

Introducing Publish Studio: Power Digital Content Creation

Say “Hi” to Publish Studio, a platform I’ve building for the past few months. If you are a content writer, then you should definitely check it out. And if you are someone who has an audience on multiple blogging platforms and need an easy way to manage your content across platforms, then you should 100% give it a try.

Let's Build a Full-Stack App with tRPC and Next.js 14

    10 min read

Let's Build a Full-Stack App with tRPC and Next.js 14

Are you a typescript nerd looking to up your full-stack game? Then this guide is for you. The traditional way to share types of your API endpoints is to generate schemas and share them with the front end or other servers. However, this can be a time-consuming and inefficient process. What if I tell you there's a better way to do this? What if I tell you, you can just write the endpoints and your frontend automatically gets the types?

Subscribe to Newsletter

Weekly


  • Never miss an update.
  • Get articles and snippets directly to your inbox.
  • Subscribe to stay connected and avoid getting lost among millions of websites.

Monthly


  • Coming soon...