How to Connect Backend to Frontend: A Complete Guide with POST Request Example
In modern web development, building a full-stack application means handling both frontend and backend. The frontend is what users interact with—buttons, forms, and visuals. The backend handles data, logic, and connections with databases.
This guide will walk you through how to connect your backend and frontend, specifically focusing on making a POST request from the frontend to the backend using JavaScript and Node.js with Express.
What Is a POST Request?
A POST request is one of the HTTP methods used to send data to a server. For example, when you submit a login form, your email and password are sent using a POST request to the backend for verification.
Basic Setup: Tools You'll Need
Frontend: HTML, JavaScript
Backend: Node.js, Express.js
Database (optional): MongoDB (with Mongoose)
Postman (optional): For testing APIs
VS Code or any code editor
Project Structure
Step 1: Setting Up the Backend (Node.js + Express)
1.1 Initialize Node Project
Open your terminal and run:
1.2 Create server.js
Inside backend/
, create a file called server.js
:
This simple server listens for a POST request at /api/data
.
Step 2: Setting Up the Frontend
2.1 Create index.html
Create a folder called frontend/
and a file index.html
inside it:
This HTML page has a form that sends data to the backend using JavaScript fetch()
.
Step 3: Connecting Backend to Database (Optional - MongoDB)
Let’s now store the data in a MongoDB database.
3.1 Setup MongoDB
Create db.js
in backend/
:
3.2 Create Schema and Model
Inside server.js
, add:
Now, whenever a POST request is received, the backend saves the data to MongoDB.
Step 4: Running the Application
Start Backend
Open Frontend
Open frontend/index.html
in your browser (just double-click it or use Live Server if using VS Code).
Submit Form
Fill in the name and email, hit “Send.” Check your terminal—you should see the POST data logged or saved to MongoDB.
Step 5: Testing with Postman (Optional)
Install Postman and make a POST request to:
Body (raw JSON):
You should get a success message back.
Common Issues and Solutions
Issue | Solution |
---|---|
CORS error in browser | Use cors() middleware in Express |
Server not receiving data | Ensure body-parser or express.json() is used |
MongoDB connection error | Make sure MongoDB is running locally or use Atlas |
Fetch API errors in frontend | Check for typos in the fetch URL or wrong port |
Frontend not refreshing after POST | Use DOM manipulation to show response messages |
Best Practices
Validate input on both frontend and backend.
Use
.env
for sensitive config like DB URIs.Use try-catch blocks for error handling.
Create a separate route file for scalability.
Use Mongoose models to structure data better.
Conclusion
Congratulations! You’ve just learned how to connect a backend to a frontend using a POST request. You saw how to:
Create a basic frontend with an HTML form
Handle a POST request using JavaScript’s
fetch()
Set up a backend with Express
Connect to a MongoDB database and save data
This foundational knowledge is essential for full-stack development. You can now build more complex apps like user authentication systems, CRUD applications, and more!