1.0.0 • Published 1 year ago

mongose-crud-templete v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

mongoose crud templete

npm

This project is an Express.js mongoose and folder setup  template for building a backend server that performs CRUD (Create, Read, Update,Delete)operations using a MongoDB database.

Installation

🖱 # run this command to setup 
 npm i mongoose crud templete
 npm i express cors bcrypt dotenv nodemon mongoose
🖱# Create the ".env" file and copy the "example .env" data to the .env file and replace the "URLS"
🖱# package.json file "dev": "nodemon server.js",
 npm run dev

🔑 Key Features:

⚙️ Database Connection:

mongodb atlas
or
mongodb compus
"Express.js Server:"
Sets up an "Express.js" server to handle HTTP requests on specified routes.
"CRUD operations:"
Routes are defined for basic "CRUD" operations on a hypothetical "Users" collection via Mongoose.
"Endpoints:"
GET /: Returns a welcome message indicating that the backend is operational.
GET /api/getAllData: A placeholder endpoint for fetching a list of users.
GET /getSingleData/:id: A placeholder endpoint for fetching details of a specific user by ID.
POST /api/create: A placeholder endpoint for creating a new user.
PUT/PATCH /api/updateData/:id: A placeholder endpoint to update a specific user by ID.
DELETE /api/idBaseDataDelete/:id: A placeholder endpoint for deleting a user by ID.
"Global Error Handling:"
Basic error handling is incorporated to catch and log any errors during database connection and server operation.
📋 Note : Ensure that a .env file set up, and replace the all Credentials.

Description:

optional

if(!userData){ 
         return res.status(404).json({msg: "User data not found"});
}

👇 মানে হচ্ছে যদি userData এর মধ্যে কোনো data আসেনি বা আগে থেকেই data নেই তাহলে json ফরম্যাটে একটা এরর দিবে। It means if there is no data in userData or there is no data already then it will give an error in json format.

controller:

req.body

👇 ক্লায়েন্ট সাইট থেকে যে ডাটা গুলো পাঠাবো সেই ডাটাগুলো req.body মধ্যে আসবে। আবার req.body এর ডাটা গুলো স্কিমা মধ্যে পাঠাবো। আবার স্কিমা সেই ডাটা গুলো ভ্যালিট কি না চেক করে mongodb এর কালেকশন এর মধ্যে পাঠাবে।

const hashPassword = await bcrypt.hash(req.body.password, 10)

👇 এই লাইনে, ব্যবহারকারীর পাসওয়ার্ডকে bcrypt হ্যাশিং এর মাধ্যমে হ্যাশ করা হয়। এটি কেয়ার পাসওয়ার্ড হ্যাশিং এর জন্য 10 রাউন্ড ব্যবহার করে।

  req.body.password = hashPassword 

এই লাইনে, হ্যাশিং করা পাসওয়ার্ডটি ব্যবহারকারীর পাসওয়ার্ড ফিল্ডে প্রতিস্থাপন করা হয়।

আপনি যেই api নিয়ে কাজ করবেন সেটা সেই controller folder গিয়ে কাজ করবেন। মানে যদি আপনার একটা api কে Delete, post, get, getSignData/: id, put /patch করতে লাগে/লাগতেছে তাহলে সেই কন্ট্রোলার ফোল্ডার এ গিয়ে একজায় Delete , post , get,getSignData/: id, put /patch এর কাজটি করবেন । এবং server.js এ গিয়ে

app.use("/api" , যেই controller  তা যে router  folder  require করেছি সেই  router  টা  এখানে  require  করে  দিবো। )

server এ একবার সেই api router app.use এ বসালে ( Delete , post , get, getSignData/: id , put /patch) এর সব কাজ করতে পারবে।

Contact:

Linkedin - Tomor-chandra-roy Email- tomorchandraroyjob943@gmail.com

Acknowledgments

Use this space to list resources you find helpful and would like to give credit to. I've included a few of my favorites to kick things off!