My Node.js Media API
This is a Node.js API that allows users to take posts . Users can create, read, update, and delete posts using this API.
To install and run the API, follow these steps:
- Clone the repository
git clone https://github.com/im-ghost/media-api.git cd media-api - Install the required dependencies by running
yarn - Create a .env file in the root directory and add your environment variables
MONGO_URI = SECRET = PORT = - Run the API using
yarn server
To use the API, make requests to the following endpoints:
-
/api/v1/users/(GET, POST) -
/api/v1/users/user/:id(PUT, DELETE, GET) -
/api/v1/users/login(POST) -
/api/v1/users/:id/follow(POST) -
/api/v1/posts/(GET, POST) -
/api/v1/posts/:id(GET) -
/api/v1/posts/:id/like(PUT) -
/api/v1/posts/:id/comment(PUT) -
/api/v1/posts/:id/:author(DELETE,PUT) -
/api/v1/posts/user/:id(GET)
All requests must include a valid JWT token in the authorization header.
GET /api/v1/users/: Get all users for the authenticated user.POST /api/v1/users: Create a new user.
GET /api/v1/users/user/:id: Get a specific user by ID for the authenticated user.PUT /api/v1/users/user/:id: Update a specific user by ID for the authenticated and authorized user.DELETE /api/v1/users/user/:id: Delete a specific user by ID for the authenticated and authorized user.
POST /api/v1/users/user/login: logs user in.
POST /api/v1/users/:id/follow: To follow a user.
POST /api/v1/posts: Create a new post.GET /api/v1/posts: Get all post.
GET /api/v1/post/post/:id: Get a specific post by ID for the authenticated user.
-
PUT /api/v1/post/:id/:author: Edit a specific post by ID and author id for the authenticated user. -
DELETE /api/v1/post/post/:id: delete a specific post by ID and author's id for the authenticated user.
GET /api/v1/post/:id/like: Like a specific post by ID for the authenticated user.
GET /api/v1/post/:id/comment: Comment on a specific post by ID for the authenticated user.
Here's an example of how to use the API to create a new post:
const fetch = require('node-fetch');
const data = {
content:"..."
authorId:`${user._id}`};
fetch('http://localhost:4000/api/v1/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'authorization': `${TOKEN}`
},
body: JSON.stringify(data),
})
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error(err));