Please check the below api built by using node.js

Post related Api



//search post

router.get(’/search’,async (req,res)=>{
if (!req.query.caption && !req.query.tag) {
return res.status(400).json(‘Please enter either title of a tag’)
}
let posts=;
if (req.query.caption) {
const regex = new RegExp(req.query.caption, “i”);
posts = await Post.find({ caption: regex });
}

  if (req.query.tag) {
    posts = posts.concat([await Post.find({ tags: req.query.tag })]);
  }

 res.json(posts)
});
//toggle save

router.get(’:/id/toggleSave’,async (req,res)=>{
const post = await Post.findById(req.params.id);

if (!post) {
return res.status(404).json(‘Post not found’)
}

const { user } = req;

if (user.savedPosts.includes(req.params.id)) {
console.log(“removing saved post”);
await User.findByIdAndUpdate(user.id, {
$pull: { savedPosts: req.params.id },
});
} else {
console.log(“saving post”);
await User.findByIdAndUpdate(user.id, {
$push: { savedPosts: req.params.id },
});
}

res.json(post);
})

PROFILE RELATED API:=>

//@route Get api/profile
//does get all profiles
//@acess Public
router.get("/",async (req,res)=>{
try {
let profiles = await Profile.find().lean().exec();

    profiles.forEach((profile) => {
      profile.isFollowing = false;
      const followers = profile.followers.map((follower) => follower._id.toString());
      if (followers.includes(req.profile.id)) {
        profile.isFollowing = true;
      }
    });
  
    profiles = profiles.filter((profile) => profile._id.toString() !== req.profile.id);
    res.json(profiles);

} catch (err) {
    console.error(err.message);
    res.status(500).send('Server error')
    
}
});
//@route Get api/profile
//does get profiles by user id
//@acess Private

router.get("/user/:user_id",async (req,res)=>{
try {
const profile=await Profile.findOne({user:req.params.user_id})(‘user’,
[‘name’,‘avatar’,‘followers’,‘following’,‘posts’]);

    if(!profile) return res.status(400).json({msg:"There is no profile for this user"});
    //otherwise
    else


    //show follow  following button

    profile.isFollowing = false;
    const followers = profile.followers.map((follower) => follower._id.toString());
  
    profile.followers.forEach((follower) => {
      follower.isFollowing = false;
      if (req.profile.following.includes(follower._id.toString())) {
        follower.isFollowing = true;
      }
    });
  
    profile.following.forEach((profile) => {
      profile.isFollowing = false;
      if (req.profile.following.includes(profile._id.toString())) {
        profile.isFollowing = true;
      }
    });
  
    if (followers.includes(req.profile.id)) {
      profile.isFollowing = true;
    }
  
    profile.isMe = req.profile.id === profile._id.toString();
  
    res.json(profile);
} catch (err) {
    console.error(err.message);
    if(err.kind==='ObjectId'){
        return res.status(400).json({msg:"Profile not found"});
    }
    res.status(500).send('Server error')
    
}
});

//follow and following
router.put('/:id/follow',async (req,res)=>{
    const user = await User.findById(req.params.id);

    if (!user) {
      return res.status(404).json('User not found');
    }
  
    // make the sure the user is not the logged in user
    if (req.params.id === req.user.id) {
      return res.status(400).json("You can't unfollow/follow yourself")
    }
  
    // only follow if the user is not following already
    if (user.followers.includes(req.user.id)) {
      return res.status(400).json('User already followed')
    }
  
    await User.findByIdAndUpdate(req.params.id, {
      $push: { followers: req.user.id },
      $inc: { followersCount: 1 },
    });
    await User.findByIdAndUpdate(req.user.id, {
      $push: { following: req.params.id },
      $inc: { followingCount: 1 },
    });
  
    res.json(user);
  });

  //unfollow
router.put(’:/id/unfollow’,async (req,res)=>{
const user = await User.findById(req.params.id);

if (!user) {
  return res.status(404).json('User not found')
}

// make the sure the user is not the logged in user
if (req.params.id === req.user.id) {
  return next({ message: "You can't follow/unfollow yourself", status: 400 });
}

await User.findByIdAndUpdate(req.params.id, {
  $pull: { followers: req.user.id },
  $inc: { followersCount: -1 },
});
await User.findByIdAndUpdate(req.user.id, {
  $pull: { following: req.params.id },
  $inc: { followingCount: -1 },
});
})







Hello there.

Do you have a question?

If so, please edit your post to include it.

The more information you give us, the more likely we are to be able to help.

Hey,thanks for replying,i am begineer in full stack webdevelopment .i wrote these api routes but dont know weather it will work or not.As defined above these are instagram features of follow,unfollw,search, togglesave a post.I am not able to check them on postman as i dont know how to check sucj routes in postman.

I just wanna know ,the http requests i used and the code of each route is correct or not.

As u will read the code carefully u will come to know which route targeting which feature.

Thnks for your help!

The best way to learn is to figure out how to test out the features yourself.

We might say it is right, but might miss a bug which can only be detectable on runtime.

But if you want to ask a specific question like maybe, is my coding style right or how to do a particular thing we can help you out

You can start by reading the official tutorial of postman itself:

https://learning.postman.com/docs/getting-started/introduction/

If you are not able to understand a feature of it or need help figuring a technical part you can ask.

Ok thanku sir!
I will learn the postman for this.

How is my coding style?
Should i use controllers or routing is enough?