,

Building Follow and Unfollow Functionality with Node.js, Express.js, MongoDB, and Restful APIs

Posted by

Follow and Unfollow APIs using Node.js, Express.js, MongoDB and Restful APIs

Follow and Unfollow APIs using Node.js, Express.js, MongoDB and Restful APIs

In this article, we will discuss how to create follow and unfollow APIs using Node.js, Express.js, MongoDB and Restful APIs. We will also explore the process of setting up the backend and implementing the APIs for following and unfollowing users in a social media platform.

Setting up the environment

First, we need to install Node.js and MongoDB on our machine. After that, we can create a new directory for our project and initialize it with npm. We can then install the necessary packages such as express, mongoose, and body-parser using npm.

Creating the database schema

We will create a new file called userModel.js and define the following schema for the user collection in MongoDB:

“`javascript
const mongoose = require(‘mongoose’);

const userSchema = new mongoose.Schema({
username: {
type: String,
unique: true
},
followers: {
type: [mongoose.Schema.Types.ObjectId],
ref: ‘User’
},
following: {
type: [mongoose.Schema.Types.ObjectId],
ref: ‘User’
}
});

module.exports = mongoose.model(‘User’, userSchema);
“`

Implementing the follow and unfollow APIs

Next, we will create a new file called userController.js and implement the follow and unfollow APIs using Express.js. We will use the POST method to implement the follow API and the DELETE method to implement the unfollow API.

“`javascript
const express = require(‘express’);
const router = express.Router();
const User = require(‘../models/userModel’);

// Follow a user
router.post(‘/follow’, async (req, res) => {
try {
const user = await User.findById(req.body.userId);
const followUser = await User.findById(req.body.followId);

if (!user.following.includes(followUser._id)) {
user.following.push(followUser._id);
await user.save();

followUser.followers.push(user._id);
await followUser.save();

res.status(200).json({ message: ‘Successfully followed user’ });
} else {
res.status(400).json({ message: ‘User already followed’ });
}
} catch (err) {
res.status(500).json({ message: err.message });
}
});

// Unfollow a user
router.delete(‘/unfollow’, async (req, res) => {
try {
const user = await User.findById(req.body.userId);
const unfollowUser = await User.findById(req.body.unfollowId);

if (user.following.includes(unfollowUser._id)) {
user.following = user.following.filter(item => item.toString() !== unfollowUser._id.toString());
await user.save();

unfollowUser.followers = unfollowUser.followers.filter(item => item.toString() !== user._id.toString());
await unfollowUser.save();

res.status(200).json({ message: ‘Successfully unfollowed user’ });
} else {
res.status(400).json({ message: ‘User not followed’ });
}
} catch (err) {
res.status(500).json({ message: err.message });
}
});

module.exports = router;
“`

Using the APIs in a client application

Once the APIs are implemented, we can use them in a client application using RESTful API calls. For example, we can send a POST request to /follow to follow a user, and a DELETE request to /unfollow to unfollow a user. We can also use the user’s ID and the target user’s ID as the request body to perform the follow and unfollow actions.

Conclusion

In this article, we have discussed how to create follow and unfollow APIs using Node.js, Express.js, MongoDB and Restful APIs. We have also explored the process of setting up the backend and implementing the APIs for following and unfollowing users in a social media platform. By following these steps, you can easily implement follow and unfollow functionality in your own applications.

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@rakeshkumarparida6362
11 months ago

Please add a tutorial on filter & search api