An Express.js + MongoDB app exposing API endpoints for a social media service using JSON Web Tokens for authentication.
-
POST /signup
Register new user- Request
- Form data
username
: <username>password
: <password>
- Form data
- Response
201
Sample response
{ "message": "Signed up successfully!", "user": { "_id": "620a9df64b80e4d747b9f94e", "username": "johndoe", "follows": [], "__v": 0 }, "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3R1c2VyMjIyd3FkcyIsImlkIjoiNjIwYTlkZjY0YjgwZTRkNzQ3YjlmOTRlIiwiaWF0IjoxNjQ0ODYyOTY2LCJleHAiOjE2NDQ5NDkzNjZ9.780_pR5NENaYXB_wP5S8nc04vqSTZi7wLZreQxF9_vs" }
- Request
-
POST /signin
Login existing user- Request
- Form data
username
: <username>password
: <password>
- Form data
- Response
200
Sample response
{ "message": "Logged in successfully", "user": { "_id": "6209541e23a674980bf81f40", "username": "wkinzu", "follows": [], "__v": 0 }, "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IndraW56dSIsImlkIjoiNjIwOTU0MWUyM2E2NzQ5ODBiZjgxZjQwIiwiaWF0IjoxNjQ0ODM1NjU4LCJleHAiOjE2NDQ5MjIwNTh9.eeHVtsdx1XCsb_aHx23F44B1vey1jVKPGPdLTw9-9Ek" }
- Request
-
GET /user/:username
View details of a user- Request
- Path parameters
:username
- Path parameters
- Response
200
Sample response
{ "data": { "_id": "6209541e23a674980bf81f40", "username": "wkinzu", "follows": [], "__v": 0 } }
- Request
-
GET /user/:username/posts
View posts from a particular user, 10 at a time- Request
- Path parameters
:username
- Query parameters
page
= <page number>
(Optional; defaults to 1)
- Path parameters
- Response
200
Sample response
{ "data": { "posts": [ { "_id": "620a34a91b3b61072805398f", "title": "A note on social media", "content": "we should take back control of SM into our own hands. freedom for all ", "creator": "6209541e23a674980bf81f40", "likedBy": [], "comments": [ { "content": "yes, I absolutely agree", "creator": "620a2b231b3b610728053989", "_id": "620a3cd8948eb92ce1d5a3fd" } ], "__v": 1, "likes": [] }, { "likes": [], "_id": "6209547523a674980bf81f42", "title": "first day of my life...", "content": "...was not too bad actually just went fine", "creator": "6209541e23a674980bf81f40", "likedBy": [], "comments": [], "__v": 0 } ], "currentPage": 1, "totalPages": 1 } }
- Request
-
GET /posts/:postId
View a particular post- Request
- Path parameters
:postId
- Path parameters
- Response
200
Sample response
{ "data": { "likes": [], "_id": "620a34a91b3b61072805398f", "title": "so this happened", "content": "someone spilled their coffee over my coat at the cafe yesterday", "creator": "6209541e23a674980bf81f40", "likedBy": [], "comments": [], "__v": 0 } }
- Request
-
GET /posts/:postId/comments
View comments of a particular post- Request
- Path parameters
:postId
- Path parameters
- Response
200
Sample response
{ "data": [ { "content": "yes, I absolutely agree", "creator": "620a2b231b3b610728053989", "_id": "620a3cd8948eb92ce1d5a3fd" } ] }
- Request
-
POST /user/:username/follows
Make a user follow/unfollow the target user- Request
- Headers
Authorization
: Bearer <user token>
- Path parameters
:username
(username with which the client is authenticated)
- Form data
targetUserId
: <userId of user to follow>
- Headers
- Response
200
Sample Response
{ "data": { "_id": "620a2b231b3b610728053989", "username": "jonhdoe", "follows": [ "620954c023a674980bf81f46" ], "__v": 3 } }
- Request
-
GET /posts
View feed i.e. posts from the followed users, 10 at a time- Request
- Headers
Authorization
: Bearer <user token>
- Query parameters
page
= <page number>
(Optional; defaults to 1)
- Headers
- Response
200
Sample Response
{ "data": { "posts": [ { "_id": "620a42bf948eb92ce1d5a437", "title": "song recommendations", "content": "I just found this new song: xx by shelter", "creator": "620954c023a674980bf81f46", "likes": [], "comments": [], "__v": 0 }, { "_id": "620a34a91b3b61072805398f", "title": "A note on social media", "content": "we should take back control of SM into our own hands. freedom for all ", "creator": "6209541e23a674980bf81f40", "comments": [ { "content": "yes, i think we should move to mastodon", "creator": "620a2b231b3b610728053989", "_id": "620a4204948eb92ce1d5a42e" } ], "__v": 12, "likes": [ "620a2b231b3b610728053989" ] }, { "_id": "6209547523a674980bf81f42", "title": "first day of my life...", "content": "...was not too bad actually just went fine", "creator": "6209541e23a674980bf81f40", "comments": [], "__v": 5, "likes": [ "620954c023a674980bf81f46" ] } ], "currentPage": 1, "totalPages": 1 } }
- Request
-
POST /posts
Create a new post- Request
- Headers
Authorization
: Bearer <user token>
- Form data
title
: <title of the post>content
: <content of the post>
- Headers
- Response
201
Sample response
{ "data": { "title": "car damage", "content": "someone in the parking yesterday hit my car, looks accidentally.", "creator": "620a2b231b3b610728053989", "likes": [], "_id": "620a3eab948eb92ce1d5a404", "comments": [], "__v": 0 } }
- Request
-
PUT /posts/:postId
Update an existing post- Request
- Headers
Authorization
: Bearer <user token>
- Path parameters
:postId
- Form data
content
: <new content of the post>
- Headers
- Response
200
Sample response
{ "data": { "acknowledged": true, "modifiedCount": 1, "upsertedId": null, "upsertedCount": 0, "matchedCount": 1 } }
- Request
DELETE /posts/:postId
Delete an existing post- Request
- Headers
Authorization
: Bearer <user token>
- Path parameters
:postId
- Headers
- Response
204
- Request
-
POST /posts/:postId/comments
Create a new comment on a post- Request
- Headers
Authorization
: Bearer <user token>
- Path parameters
:postId
- Form data
content
: <content of the commment>
- Headers
- Response
201
Sample response
{ "data": { "likes": [], "_id": "620a34a91b3b61072805398f", "title": "A note on social media", "content": "we should take back control of SM into our own hands. freedom for all ", "creator": "6209541e23a674980bf81f40", "likedBy": [], "comments": [ { "content": "yes, I absolutely agree", "creator": "620a2b231b3b610728053989", "_id": "620a3cd8948eb92ce1d5a3fd" } ], "__v": 1 } }
- Request
DELETE /posts/:postId/comments/:commentId
Delete an existing comment on a post- Request
- Headers
Authorization
: Bearer <user token>
- Path parameters
:postId
:commentId
- Headers
- Response
204
- Request
-
POST /posts/:postId/likes
Like/dislike a post- Request
- Headers
Authorization
: Bearer <user token>
- Path parameters
:postId
- Headers
- Response
200
Sample response
{ "data": { "_id": "620a34a91b3b61072805398f", "title": "A note on social media", "content": "we should take back control of SM into our own hands. freedom for all ", "creator": "6209541e23a674980bf81f40", "likedBy": [], "comments": [], "__v": 9, "likes": [ "620a2b231b3b610728053989" ] } }
- Request