Hey developers! In this article, we will cover all the things that are shown on the "Simple MERN Stack E-Commerce Product CRUD Project" that is uploaded to the "Code with Zihad" YouTube channel. So, following this article, you will be able to practice and make the project on your own through understanding all concepts. I will provide the context of why we are doing this or that and the code snippet also. So that anyone can understand and get the highest value from the article.
What are the prerequisites for starting to build the project?
Ans: Honestly, you have to learn JavaScript and the fundamentals of MERN stack so that you can understand all the things perfectly. Surely I will explain to you all the things from project setup to completing the project in a beginner-friendly way. But you have to learn JavaScript and MERN Stack basics first to get started with the project.
Note: Some people start with frontend and some with backend. My preference is to start with the backend that we can simply use the APIs in the frontend without using any static or demo data.
Now let's start and build the project "simple MERN Stack e-commerce product CRUD application where we can create, read, update, and delete the product with realtime functionality + search product filtering. so we will use Express to build the server instead of using plain Node.js because it's a little bit complex. and we will use ODM (Object Document Model) Mongoose to declare a schema as we want and using that schema we can easily run CRUD operation to MongoDB. Don't worry If you didn't used it, I will show how to do that.
To create the server create a folder and open it with terminal and run npm init --y
it will create a package.json file for our server and then we need to install packages for that run npm install express mongoose dotenv cors
- with these we have installed all necessary packages we need to build our server. Run echo > server.js
in your terminal to create server.js file you can create it anyway.
Now we have to make some changes in package.json following the code snippet below:
Simply we have to change the property "main": "index.js" to "server.js" and added another property "type": "module"
that will enable the ES Module syntax. Inside scripts object we have to add a shortcut (you can say), that is "server": "node server.js"
We will follow structured folder pattern. Although we are building simple backend but we are gonna follow beginner friendly pattern.
Folder examples given below:
1. config - the config folder stores app configuration files, we will connect our database inside config folder in db.js file
2. models - where schemas are created using ODM / ORM, for our case we will use mongoose ODM and create a product schema
3. controllers - stores the controller file where the main function or business logic, incoming request and response are handled.
4. routes - for defining endpoints and point to controller that which endpoint is connected to which controller
There are many ways for structuring files, as long it is for beginners so this approach will helpful.
config/db.js
Import mongoose and write a connectDB function and await mongoose.connect(your_mongodb_uri) will connect to you database. and write a console.log() that you can know your db is connected or occured any error. then export the function that you can access this file from another file.
server.js
Here we have added dotenv.config() that we can use .env files variable in our app. Then we have called our connectDB function that we have written in config/db.js file. and express.json() is for supporting json from request body like body-parser and cors() is for allowing the server to use from react frontend. and also we have declared a port 5000.
models/product.model.js
We are using Mongoose as ODM (Object Document Model) for create a desired schema. It will help us to prevent unnecessary data from inserting to our database. and It will work as a server side data validation, if you use required: true then it will be must required field, if someone don't input any data on that field it will throw validation error from mongoose. and we can easily do CRUD operations using the model.
controllers/product.controller.js
All operation logics, request and response are handled here. so with that we have created our APIs. now will define endpoints for the APIs.
routes/product.route.js
Now we have defined our end-points and exported router as ProductRoutes. lastly we have to change our server.js for handling our router in our application using app.use() middleware.
server.js
Great! Now open your server directory in your terminal and run the script that we have written at the first. npm run server
to start the server.
Now you can test all the endpoints using Postman or Thunder Client. If all APIs working well then move forward to the client (fronted). If there is any issue then make sure you have done all the things correctly. you can also checkout the video on "Code with Zihad" YouTube channel.
It's time to setup client meaning our JavaScript frontend framework React.js and Integrate the server (APIs) that we have previously build. To install react using vite run command npm create vite@latest client
and select template react and variant javascript.
It will create client folder and initialized necessary packages inside it. Now change directory to client run cd client
and run npm install axios
to install all necessary packages and axios. we will use axios instead of fetch method to handle http requests.
For styling our web we will use CSS framework (Tailwind CSS). To Install it run following commands:
It will create tailwind.config.js that you have to modify following code snippet below:
and add following directives to index.css at the top
with these we have done our client setup. now we are good to dive into our main task. All we need is to create a components folder inside src directory. Now we have to create ProductForm.jsx and ProductList.jsx and add these components to App.jsx and modify itself also.
src/App.jsx
src/components/ProductForm.jsx
src/components/ProductList.jsx
✅Great! npm run dev
It's live locally. If you have done all the things correctly It will work. If there is any errors or issue please checkout the video form of this project on "Code with Zihad" YouTube channel.
You can add more functionality to this project like authentication, authorization & many more. Give your own touch to the project and unlock your creativity. Make sure to deploy the project to share it publicly.