Run the server Side and Client simultaneously in NodeJS application

When we setting up our web application in our node js project, we usually have two folders named as client and server, So to run out application we have to run both server and client separately for example react js and node js project

to run out react project we need to fire a command :

npm start

And to start our node js server we need to file

node sever.js

So lets discuss the issues we can face, if we run both the things separately :

1. Manual Process Management
  • Problem: Without concurrently running both the server and the client, you’ll need to manually start both the server and the client in separate terminal windows. This increases the risk of forgetting to start one of them, leading to confusion and wasted time during development.
  • Solution: Automating the process using a command like "dev" ensures both are started together, making the development process smoother.
2. Synchronization Issues
  • Problem: If you start the server and client separately without coordination, there might be synchronization issues. For example, changes in the client or server code might not reflect properly if one of the processes fails or restarts unexpectedly.
  • Solution: Using a tool like concurrently allows both processes to be managed together, ensuring that they start and stop simultaneously, which can help avoid such issues.
3. Increased Complexity in Workflow
  • Problem: Having to run multiple commands for starting the server and client adds unnecessary complexity to your development workflow. It also makes the project less accessible to other developers who might not be familiar with the setup.
  • Solution: A single command simplifies the workflow, making it easier for you and other developers to start the project with minimal effort.
4. Environment Variable Management
  • Problem: Running the server and client separately might require different sets of environment variables, which can be cumbersome to manage. If they are not aligned properly, you may run into issues where the client is pointing to the wrong server URL or the server is not recognizing certain configurations.
  • Solution: Running both processes together ensures that they share the same environment and configuration, reducing the likelihood of such issues.
5. Integrated Error Handling
  • Problem: When running the server and client separately, you won’t have an integrated view of the logs and errors. This can make debugging more difficult, as you might need to check multiple terminal windows to trace the source of an issue.
  • Solution: Using a tool like concurrently can streamline error handling by displaying logs and errors from both the server and client in a single terminal window, making it easier to identify and resolve issues.
6. Developer Experience
  • Problem: Without automation, the development experience can become more cumbersome and error-prone, leading to slower development cycles and frustration.
  • Solution: Streamlining the process with a "dev" command enhances the developer experience, allowing for faster iteration and reducing the likelihood of mistakes.

Now Let go to the solution how we can run the server Side and Client simultaneously in NodeJS application

Run node js and react js in same project simultaneously

If your client/front-end and server/back-end folders are separated in the root directory, you can run both the front-end and back-end servers together at the same time using concurrently. A few things need to be done before this is possible:
let start with node js and react js sample project

  1. CD into your root directory
  2. npm init -y (enter)
  3. touch .gitignore (enter)
  4. Add /node_modules to the .gitignore file
  5. npm install concurrently nodemon --save-dev (enter)
  6. Install all your backend server dependencies in the root directory
  7. Add the three scripts below to your root package.json:

Run react and node js with single command

Both the client and backend scripts are ran together by the dev script from the root directory when the npm run dev command is executed in the terminal. The dev script traverses both the client and server directories, locates the directories index.js file, and then runs each server.

Further reading

  • React UseEffect | simplifyScript

    Understanding the useEffect hook in React

    In this article, we will learn about another React hook called useEffect(), through this article you will be able to understand what is react useEffect Hook and how to use useEffect() hook in react, this is most communally used hook after useState() hook, we mostly use this hook in every component on react now first […]
  • React useState function

    How to Use useState Hook in React Application

     In a React application, normal vanilla JavaScript may not work for some concepts. For example, if I want to toggle a class on a button click or add and remove classes, I might need to show a popup on the website. This can be easily achieved in vanilla JavaScript using the element.classList.toggle(“newClass”) method, allowing us to add […]
  • javascript DOm manipulation - simplifyscript

    What is DOM manipulation in javascript 

    In this article, we will explore JavaScript DOM manipulation, a key feature of JavaScript that allows developers to modify the Document Object Model (DOM) of a webpage. DOM manipulation enables you to dynamically alter the content and structure of HTML documents. To grasp this concept thoroughly, we’ll start by defining what the DOM is and […]