Loading...

Connect Node.js and React: Build Your First Full-Stack App

If you are learning web dev, you already know a bit of Node.js (backend) or React (frontend). But guess what? πŸš€ Real projects need both working together. In this guide, we will build a very simple full-stack app by connecting Node.js API with a React frontend.

πŸ€” What is Full-Stack?

  • Backend (Node.js) β†’ talks to database, handles API.

  • Frontend (React) β†’ shows the data to the user in browser.

When backend + frontend = ❀️ β†’ you get a full-stack app.

πŸ›  Step 1: Make a Small Node.js API

Let’s start with backend. Create a file server.js:
const express = require("express");
const app = express();
const PORT = 5000;

app.get("/api/message", (req, res) => {
  res.json({ message: "Hello from Node.js backend!" });
});

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

Run this:
npm init -y
npm install express
node server.js
πŸ‘‰ Open http://localhost:5000/api/message and you’ll see JSON response.

βš›οΈ Step 2: Create a React App

Now the frontend part. Make a new React app:
npx create-react-app myapp
cd myapp
npm start
This opens http://localhost:3000 with a starter React page.

πŸ”— Step 3: Connect React to Node.js

Edit App.js like this:
import { useEffect, useState } from "react";

function App() {
  const [msg, setMsg] = useState("");

  useEffect(() => {
    fetch("http://localhost:5000/api/message")
      .then(res => res.json())
      .then(data => setMsg(data.message));
  }, []);

  return (
    <div>
      <h1>Full Stack App</h1>
      <p>{msg}</p>
    </div>
  );
}

export default App;
export default App;

πŸ‘€ Step 4: Test It Out

  1. Start backend β†’ node server.js (port 5000)

  2. Start frontend β†’ npm start (port 3000)

  3. Open browser β†’ http://localhost:3000

πŸŽ‰ You will see: β€œHello from Node.js backend!”

πŸš€ Why This Matters

  • Node.js = API / data provider

  • React = UI / data viewer

  • Together β†’ you created your first full-stack app

This is just the start. Next, you can add database (MongoDB, MySQL), more routes, or even deploy online.

πŸ”— References & Contact

If you want to learn more, check out the official docs for Node.js and React. You can also read my related blogs: How to Build Your First Node.js API and What is React: My Way of Understanding It.

For questions, feedback, or just to say hi πŸ‘‹, you can reach me at info@vaibhavjha.in,Β or on LinkedIn @vaibhavjha08.

Leave a Reply

Your email address will not be published. Required fields are marked *