Community Marketplace App using MERN Stack
Last Updated :
17 Sep, 2024
Building a community Marketplace App will help you understand the foundational concepts of full-stack development using the MERN(MongoDB, ExpressJS, React, NodeJS) stack. This tutorial will guide you to set up the backend server for the project and demonstrate the integration of frontend functionality with backend setup. This tutorial will teach you how to leverage mongoDB to create and store product profiles.
Preview
OutputPrerequisites
Approach to Create Community Marketplace App
- Define a functional component named App in 'App.js'.
- Make a list of necessary dependencies and component and import them.
- Define Nav and ProductList components and wrap them inside in the App components to provide access to shared context data
- Create Seller.js page with form component.
- Use React Router to navigate between different pages.
- Use Axios to fetch data from backend server.
- Establish connections to the MongoDB database from the backend server using Mongoose or MongoDB native drivers.
Note: You have to install MongoDB and get the URI from that so that you can connect to it and use the database respectively.
Steps to Create a Backend Server:
Step 1: Create a new directory named backend.
mkdir backend
cd backend
Step 2: Create a server using the following command in your terminal.
npm init -y
Step 3: Install the necessary package in your server using the following command.
npm install express mongoose mongodb cors
Project Structure:
project structure backendUpdated dependencies:
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.3",
"mongodb": "^6.3.0",
"mongoose": "^8.2.0",
}
Step 4: Create a file 'server.js' and set up the server.
JavaScript
// server.js
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 5000;
// MongoDB connection
mongoose.connect('mongodb://localhost:27017/myapp', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error(err));
// Define Product Schema
const productSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
description: String,
price: {
type: Number,
required: true
},
category: String,
imageUrl: String, // Directly store image URL
});
const Product = mongoose.model('Product', productSchema);
// Middleware for CORS
app.use(cors());
// Middleware for parsing JSON bodies
app.use(express.json());
// Route to fetch products
app.get('/product', async (req, res) => {
try {
const products = await Product.find();
res.json(products);
} catch (err) {
console.error(err);
res.status(500).json({ message: 'Server Error' });
}
});
// Route to add new product
app.post('/product', async (req, res) => {
try {
const { name, description, price, category, imageUrl } = req.body;
const newProduct = new Product({
name,
description,
price,
category,
imageUrl,
});
const savedProduct = await newProduct.save();
res.status(201).json(savedProduct);
} catch (err) {
console.error(err);
res.status(500).json({ message: 'Server Error' });
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Start your server using the following command.
node server.js
Steps to Create the Frontend:
Step 1: Create the frontend repository named client in the main repository.
mkdir client
cd client
Step 2: Create React project using following command.
npx create-react-app .
Step 3: Install necessary dependencies in your application using following command.
npm install axios react-router-dom
Project Structure:
frontend structureUpdated dependencies:
"dependencies": {
"axios": "^1.6.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.2",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example: Create the files according to the project structure and write the following code.
CSS
/* styles.css */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 20px auto;
padding: 0 20px;
}
h2 {
margin-bottom: 20px;
}
.product-list {
list-style: none;
padding: 0;
}
.product-item {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
}
.navbar {
background-color: #333;
color: #fff;
padding: 10px 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
.navbar-brand {
margin: 0;
}
.nav-links {
list-style: none;
padding: 0;
margin: 0;
display: flex;
}
.nav-item {
margin-right: 10px;
}
.nav-link {
color: #fff;
text-decoration: none;
}
/* Form styles */
.form-input {
width: 100%;
padding: 8px;
margin-bottom: 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid #ccc;
}
.form-submit {
background-color: #333;
color: #fff;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
}
.form-submit:hover {
background-color: #555;
}
.product-list {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.product {
flex: 1 1 calc(33.33% - 20px);
border: 1px solid #333;
padding: 3px;
}
.break-line {
width: 100%;
margin-top: 20px;
border: none;
border-top: 1px solid #ccc;
}
.product-image {
width: 300px;
height: 200px;
object-fit: cover;
border-radius: 5px;
}
JavaScript
// App.js
import React from 'react';
import {
BrowserRouter as Router,
Routes,
Route
} from 'react-router-dom';
import Nav from './Nav.js';
import ProductList from './ProductList.js';
import Seller from './Seller.js';
import './index.css'
const App = () => {
return (
<Router>
<div>
<Nav />
<Routes>
<Route path="/" element={<ProductList />} />
<Route path="/seller" element={<Seller />} />
</Routes>
</div>
</Router>
);
}
export default App;
JavaScript
// Seller.js
import React, { useState } from 'react';
import axios from 'axios';
const Seller = () => {
const [formData, setFormData] = useState({
name: '',
description: '',
price: '',
category: '',
imageUrl: '',
});
const handleSubmit = async (e) => {
e.preventDefault();
try {
const response = await axios.post('http://localhost:5000/product',
formData);
console.log(response.data);
} catch (error) {
console.error('Error:', error);
}
}
const handleChange = (e) => {
setFormData({ ...formData, [e.target.name]: e.target.value });
}
return (
<div>
<h2>Become a Seller</h2>
<form onSubmit={handleSubmit}>
<input type="text" name="name"
placeholder="Product Name" onChange={handleChange} />
<input type="text" name="description"
placeholder="Description" onChange={handleChange} />
<input type="number" name="price"
placeholder="Price" onChange={handleChange} />
<input type="text" name="category"
placeholder="Category" onChange={handleChange} />
<input type="text" name="imageUrl"
placeholder="Image URL" onChange={handleChange} />
<button type="submit">Add Product</button>
</form>
</div>
);
}
export default Seller;
JavaScript
// ProductList.js
import React, {
useState,
useEffect
} from 'react';
import axios from 'axios';
import './index.css'
const ProductList = () => {
const [products, setProducts] = useState([]);
useEffect(() => {
fetchProducts();
}, []);
const fetchProducts = async () => {
try {
const response = await axios.get('http://localhost:5000/product');
setProducts(response.data);
} catch (error) {
console.error('Error:', error);
}
}
return (
<div className="container">
<h2>Products</h2>
<div className="product-list">
{products.map((product, index) => (
<div key={product._id} className="product">
<h3>{product.name}</h3>
<img src={product.imageUrl}
alt={product.name} className="product-image" />
<p className="product-description">
Description: {product.description}
</p>
<p className="product-price">
Price: ${product.price}
</p>
<p className="product-category">
Category: {product.category}
</p>
{/* Add a line break after every third product */}
{((index + 1) % 3 === 0) &&
(<hr key={index} className="break-line" />)}
<button>Add to cart</button>
</div>
))}
</div>
</div>
);
}
export default ProductList;
JavaScript
// Nav.js
import React from 'react';
import { Link } from 'react-router-dom';
const Nav = () => {
return (
<nav className="navbar">
<h1 className="navbar-brand">
Marketplace
</h1>
<ul className="nav-links">
<li className="nav-item">
<Link to="/" className="nav-link">
Home
</Link>
</li>
<li className="nav-item">
<Link to="/seller" className="nav-link">
Become a Seller
</Link>
</li>
</ul>
</nav>
);
}
export default Nav;
Start the project using the given command.
npm start
Output:
- Output of data saved in database:
Similar Reads
Create a Community Marketplace App using React
In this article, we will explore the process of developing a Community Marketplace application using React, that offers a seamless and user-friendly experience for users. The Community Marketplace App is a platform where users can buy and sell goods. It provides a convenient way for community member
8 min read
Community Forum Page using MERN Stack
In the ever-expanding digital landscape, fostering meaningful connections within communities is paramount. The Community Forum Page project, developed using the MERN (MongoDB, Express, React, Node) stack, aims to provide a dynamic platform for users to engage in discussions, share valuable informati
8 min read
Restaurant App using MERN Stack
Creating a Restaurant app will cover a lot of features of the MERN stack. In this tutorial, we'll guide you through the process of creating a restaurant application using the MERN stack. The application will allow users to browse through a list of restaurants, view their menus, and add items to a sh
11 min read
Bookstore Ecommerce App using MERN Stack
Bookstore E-commerce project is a great way to showcase your understanding of full-stack development. In this article, we'll walk through the step-by-step process of creating a Bookstore E-commerce using the MERN (MongoDB, Express.js, React, Node.js) stack. This project will showcase how to set up a
8 min read
Community Forum Page using MEAN Stack
Creating a community forum page using the MEAN (MongoDB, Express.js, Angular, Node.js) stack will clear the concepts of MEAN stack. It will help strengthen the understanding of CRUD operations. This article will discuss about the features of creating, updating, deleting a post, like / unlike feature
15+ min read
Notes Maker App using MERN Stack
The "Notes Maker" project is an helpful web application designed to help users effectively create, manage, and organize their notes. In this article we are utilizing the MERN (MongoDB, Express, React, Node) Stack, to build a notes maker application that provides a seamless user experience for note-t
6 min read
Hospital Management Application using MERN Stack
In the fast-paced world of healthcare, it's vital to manage hospital tasks effectively to ensure top-notch patient care. This article explores creating a strong Hospital Management App using the MERN stack â that's MongoDB, Express, React, and Node.js, breaking down the process for easier understand
15+ min read
Task Manager App using MERN Stack
Task Manager is very crucial to manage your tasks. In this article, we are going to develop a task manager application using the MERN stack. This application helps users to manage their tasks efficiently, offering essential features like creating new tasks, editing existing ones, and deleting tasks
10 min read
Bookstore Ecommerce App using MEAN Stack
In this article, we will build a simple yet powerful bookstore application using Angular for the front end and Node.js for the back end. This project application will showcase how to set up a full-stack web application where users can view, filter, and purchase various books. Project Preview: Prereq
8 min read
Notes Maker App using MEAN Stack
The Notes Maker project is a helpful web application designed to help users effectively create, manage, and organize their notes. In this article we are utilizing the MEAN (MongoDB, Express, Angular, Node) Stack, to build a notes maker application that provides a seamless user experience for note-ta
7 min read