Node.js में ब्लॉग कंट्रोलर को अपडेट करना सिखें | लेक 90 | @thecodethread

Posted by


Namaste doston! Aaj ke is tutorial me hum baat karenge updating blog controller ke implementation ke baare mein Node.js me. Is tutorial me hum seekhenge ki kaise hum apne blog controller ko update kar sakte hai Node.js me.

To chaliye shuru karte hai. Sabse pehle, aapko ek blog controller file create karna hoga apne Node.js project me. Iske liye aap apne project me ek new file create kar sakte hai, jaise ki blog.controller.js.

Ab, is file me hum define karenge ek function jiska naam hoga updateBlog. Ye function humare blog ko update karne me madad karega. Yahan hum example ke taur par MongoDB ka use karenge, lekin aap apne database ke hisab se implementation kar sakte hai.

const Blog = require('../models/blog.model');

const updateBlog = async (req, res) => {
  try {
    const { id } = req.params;
    const { title, content } = req.body;

    const updatedBlog = await Blog.findByIdAndUpdate(id, { title, content }, { new: true });

    res.status(200).json(updatedBlog);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}

module.exports = { updateBlog };

Is code me hum blog model ko import kar rahe hai jo humare MongoDB database ka schema represent karta hai. Fir hum updateBlog function banate hai jo blog ko update karega. Is function me hum pehle request se blog id aur updated title aur content nikalte hai. Fir hum MongoDB ke findByIdAndUpdate method ka use karke blog ko update karte hai. Agar kuch galat hota hai, to hum error message bhejte hai.

Ab, is code ko apne existing routes me include karna hoga apne Express app me. Aap apne routes file me is tarah se include kar sakte hai.

const express = require('express');
const router = express.Router();

const { updateBlog } = require('../controllers/blog.controller');

router.put('/blogs/:id', updateBlog);

module.exports = router;

Is code me hum put method ka use kar ke /blogs/:id endpoint ko define kar rahe hai. Is endpoint me hum updateBlog function ko call karte hai.

Ab, aap apne Node.js server ko run karke is endpoint ko hit karke dekh sakte hai ki kaise aap blog ko update kar sakte hai. Bus is endpoint me blog id aur updated data pass karna hoga.

Toh doston, is tarah se aap Node.js me updating blog controller ko implement kar sakte hai. Agar aapko koi bhi sawal ya sujhav ho toh aap niche comment karke puch sakte hai. Dhanyavaad!

0 0 votes
Article Rating
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@propergamer_64
1 month ago

Best best best ❤

@udaykumar-kz2sg
1 month ago

Awesome tutorial! I’d love to see a full-stack MERN project that can really stand out on a resume and impress recruiters. Something with real-world functionality, like authentication, complex data relationships, responsive design, and a polished UI/UX. It would be great if you could cover key features like role-based access, testing, and maybe even deploying the app. This would be perfect for anyone looking to showcase their skills in a job interview! Thank You ,,,,,Happy Coding 🔥🔥

@vipulgupta4125
1 month ago

great video