#Nodejs intern assignment-3 submission To design and implement a RESTful APII for a basic social networking application using Node.js and MongoDB. The application should have the following functionalities:
- User registration
- User login
- Post a message
- Get all posts of a user
- Follow a user
- Get all followers of a user
- Get all followings of a user
- Get the posts of a user
The application has been deployed on render. The application can be accessed using the following link: https://nodejs-intern-assignment-3-submission-igb7.onrender.com
- Clone the repository
git clone https://github.com/0ju1c3/Nodejs-intern-assignment-3-submission.git
- Install the dependencies
npm install
- Start the server
npm start
The following environment variables are required for the application to run:
JWT_SECRET=your-secret-key
CONNECTION_STRING=mongodb://username:password@host:port/database?options
PORT=3000
The application can be used to create a basic social networking application. The application can be used to register users, login, post messages, follow users, get followers, get followings, and get posts of a user.
The following are the API endpoints of the application:
- User registration
Endpoint : /users/register Method: POST Description: Register a new user Request Body:
{
"username":"string",
"bio":"string",
"profilePictureUrl":"string",
"password":"string",
"confirmPassword":"string"
}
Response Format:
{
"accessToken": "string"
}
- User login Endpoint : /users/login Method: POST Description: Login a user Request Body:
{
"username":"string",
"password":"string"
}
response format:
{
"accessToken": "string"
}
- Profile Endpoint : /users/profile Method: GET Description: Get the profile of the logged in user Request Headers:
{
"Authorization":"Bearer accessToken"
}
Response Format:
{
"userId": "string",
"username": "string",
"bio": "string",
"profilePictureUrl": "string",
"NoFollowers":"number",
"NoFollowings":"number",
"NoPosts":"number"
}
- Update Profile Endpoint : /users/profile Method: PUT Description: Update the profile of the logged in user Request Headers:
{
"Authorization":"Bearer accessToken"
"bio":"string",
"profilePictureUrl":"string",
"password":"string",
}
Response Format:
{
"userId": "string",
"username": "string",
"bio": "string",
"profilePictureUrl": "string",
"NoFollowers":"number",
"NoFollowings":"number",
"NoPosts":"number"
}
- Post a message Endpoint : /posts Method: POST Description: Post a message Request Headers:
{
"Authorization":"Bearer accessToken"
"title":"string",
"content":"string"
}
Response Format:
{
"userId": "string",
"username": "string",
"title": "string",
"content": "string",
"_id": "string",
"createdAt": "string",
"updatedAt": "string"
}
- Get a post of a user Endpoint : /posts/:postId Method: GET Description: Get a post of a user Request Headers:
{
"Authorization":"Bearer accessToken"
}
Response Headers:
{
"userId": "string",
"username": "string",
"title": "string",
"content": "string",
"_id": "string",
"createdAt": "string",
"updatedAt": "string"
}
- Get all posts of a user Endpoint : /posts Method: GET Description: Get all posts of a user Request Headers:
{
"Authorization":"Bearer accessToken"
}
Response Headers:
{
{
"userId": "string",
"username": "string",
"title": "string",
"content": "string",
"_id": "string",
"createdAt": "string",
"updatedAt": "string"
},
}
- Update a post Endpoint : /posts/:postId Method: PUT Description: Update a post Request Headers:
{
"Authorization":"Bearer accessToken",
"title":"string",
"content":"string"
}
Response Headers:
{
"_id": "string",
"userId": "string",
"username": "string",
"title": "string",
"content": "string",
"createdAt": "string",
"updatedAt": "string"
}
- Delete a post Endpoint : /posts/:postId Method: DELETE Description: Delete a post Request Headers:
{
"Authorization":"Bearer accessToken"
}
Response Headers:
{
"message": "Post removed"
}
- Get all Users Endpoint : /follow Method: GET Description: Get all users Request Headers:
{
"Authorization":"Bearer accessToken"
}
Response Headers:
{
"users": [
{
"_id": "string",
"userId": "string",
"username": "string",
"bio": "string",
"profilePictureUrl": "string",
}
]
}
- Get a User Endpoint : /follow/:userId Method: GET Description: Get a user Request Headers:
{
"Authorization":"Bearer accessToken"
}
Response Headers:
{
"user": {
"_id": "string",
"userId": "string",
"username": "string",
"bio": "string",
"profilePictureUrl": "string",
"NoFollowers":"number",
"NoFollowings":"number",
"NoPosts":"number"
}
}
- Follow a user Endpoint : /follow/user/:userId Method: POST Description: Follow a user Request Headers:
{
"Authorization":"Bearer accessToken"
}
Response Headers:
{
"userId":"string",
"username":"string",
"followingId":"string",
"following":"string"
}
- Get followings of a user Endpoint : /follow/followings Method: GET Description: Get followings of a user Request Headers:
{
"Authorization":"Bearer accessToken"
}
Response Headers:
{
{
"followingId":"string",
"following":"string"
}
}
- Get followers of a user Endpoint : /follow/followers Method: GET Description: Get followers of a user Request Headers:
{
"Authorization":"Bearer accessToken"
}
Response Headers:
{
{
"followerId":"string",
"follower":"string"
}
}
- Unfollow a user Endpoint : /follow/:userId Method: DELETE Description: Unfollow a user Request Headers:
{
"Authorization":"Bearer accessToken"
}
Response Headers:
{
"message": "User unfollowed"
}
- Get posts of followings Endpoint : /follow/posts Method: GET Description: Get posts of followings Request Headers:
{
"Authorization":"Bearer accessToken"
}
Response Headers:
{
"posts": [
{
"userId": "string",
"username": "string",
"title": "string",
"content": "string",
}
]
}
The users
collection stores information about registered users.
- _id: ObjectId (automatically generated)
- userId: String (generated using uuidv4, required, unique) - Unique identifier for the user.
- username: String (required, unique) - The username of the user.
- bio: String - The biography or additional information about the user.
- profilePictureUrl: String - The URL of the user's profile picture.
- password: String (required) - The hashed password of the user.
- NoFollowers: Number (default: 0) - The number of followers for the user.
- NoFollowing: Number (default: 0) - The number of users the user is following.
- NoPosts: Number (default: 0) - The total number of posts created by the user.
- createdAt: Date (automatically generated) - The timestamp when the user was created.
- updatedAt: Date (automatically generated) - The timestamp when the user was last updated.
The posts
collection stores information about the posts created by users.
- _id: ObjectId (automatically generated)
- userId: String (required, reference to User collection) - The unique identifier of the user who created the post.
- username: String (required, reference to User collection) - The username of the user who created the post.
- title: String (required) - The title of the post.
- content: String (required) - The content or body of the post.
- createdAt: Date (automatically generated) - The timestamp when the post was created.
- updatedAt: Date (automatically generated) - The timestamp when the post was last updated.
- userId: References the
User
collection using theuserId
field. - username: References the
User
collection using theusername
field.
The follows
collection stores information about the users that a user is following and the users that are following the user.
- _id: ObjectId (automatically generated)
- followerId: String (required, reference to User collection) - The unique identifier of the follower user.
- follower: String (required, reference to User collection) - The username of the follower user.
- followingId: String (required, reference to User collection) - The unique identifier of the user being followed.
- following: String (required, reference to User collection) - The username of the user being followed.
- createdAt: Date (automatically generated) - The timestamp when the follow relationship was created.
- updatedAt: Date (automatically generated) - The timestamp when the follow relationship was last updated.
- followerId: References the
User
collection using thefollowerId
field. - follower: References the
User
collection using thefollower
field. - followingId: References the
User
collection using thefollowingId
field. - following: References the
User
collection using thefollowing
field.
The application has been tested using Collection Runner in Postman. The tests include the following:
- User registration
- User login
- User profile
- Update profile
- Delete user
- Post a message
- Update a post
- Get all Posts
- Get a post
- Delete a post
- Get all Users
- Get a User
- Follow a user
- Get followings of the current user
- Get followers of the current user
- Unfollow a user
- Get posts of followings
The tests have been written to ensure that the API endpoints are working as expected and that the responses are correct.
To run the tests, import the Node Assignment.postman_collection.json
file in the test directiory and run the collection.