Tutorial
Posts Routes 1

Post Routes

Routes with Files

We need a routes with files so when we create the post we need to allow the user to be able to upload a picture

We already discussed about another routes with files here

import { createPost } from "./controllers/posts.js";
import { verifyToken } from "./middleware/auth.js";
app.post("/posts", verifyToken, upload.single("picture"), createPost);

Breaking down the code above, we have the following:

// Import the `createPost` function from the `posts.js` module in the `controllers` directory
import { createPost } from "./controllers/posts.js";
 
// Define a route for handling HTTP POST requests to the `/posts` endpoint
app.post(
  "/posts",
  // Use the `verifyToken` middleware function to check if the user is authorized to make the request
  verifyToken,
  // Use the `upload.single()` middleware function to handle file uploads associated with the `picture` field
  upload.single("picture"),
  // Use the `createPost` function to handle the logic of the request and create a new post
  createPost
);

In summary, this code defines a route for handling a POST request to /posts that expects a single file with the name "picture" to be uploaded. Before the file is uploaded, the verifyToken middleware is called to check if the user is authenticated and authorized to upload files. The file upload is handled by the upload.single middleware, and the uploaded file is passed to the createPost controller function, which processes the file and creates a new post.

Routes for Posts

server
├── routes
│   ├── posts.js
routes/posts.js
import express from "express";
import { getFeedPosts, getUserPosts, likePost } from "../controllers/posts.js";
import { verifyToken } from "../middleware/auth.js";
 
const router = express.Router();
 
/* READ */
router.get("/", verifyToken, getFeedPosts);
router.get("/:userId/posts", verifyToken, getUserPosts);
 
/* UPDATE */
router.patch("/:id/like", verifyToken, likePost);
 
export default router;

Breaking down the code above

router.get("/", verifyToken, getFeedPosts);
  • The first function in the middleware chain is "verifyToken". This function is responsible for checking whether the user making the request is authenticated and authorized to access the requested resource.
  • If the user is authenticated, the next middleware function in the chain is called, which is "getFeedPosts".
  • The purpose of the "getFeedPosts" function is to retrieve all posts from the database and send them back as a response to the user's request.
  • In the context of a social media app, this code would be used on the home page to display all the posts from users that the logged-in user follows.
  • In a production-level app, the home page feed would be curated using complex algorithms, such as machine learning, to display relevant and engaging content to the user.
  • However, in this example, the code is simply sending back all the posts in the database.

In summary, the code snippet sets up a router to listen for GET requests on the root URL ("/"). When a user makes a request, the server first checks if the user is authenticated using the "verifyToken" middleware function. If the user is authenticated, the "getFeedPosts" middleware function retrieves all posts from the database and sends them back as a response to the user's request.

router.get("/:userId/posts", verifyToken, getUserPosts);
  • The first parameter in the router.get() method is a dynamic path that includes a placeholder variable called "userId". This variable represents the unique ID of the user whose posts we want to retrieve.
  • The second middleware function in the chain is "verifyToken", which is responsible for verifying the user's authentication and authorization to access the requested resource.
  • If the user is authenticated, the third middleware function in the chain is called, which is "getUserPosts".
  • The purpose of the "getUserPosts" function is to retrieve all posts from the database that belong to the user whose ID was specified in the request.
  • In a production-level app, this function would typically return only the most relevant and engaging posts for that user, but in this case, it simply returns all the posts that belong to that user.
  • For example, if we had a user named Steve Ralph with an ID of "123", and we made a GET request to "/123/posts", the "getUserPosts" function would retrieve all posts in the database that have "userId" equal to 123 and return them as a response to the client.

In summary, the code snippet sets up a router to listen for GET requests on the "/:userId/posts" URL path, where the "userId" parameter represents the unique ID of the user whose posts we want to retrieve. The server verifies the user's authentication and authorization using the "verifyToken" middleware function, and then retrieves all the posts from the database that belong to that user using the "getUserPosts" middleware function.

/* UPDATE */
router.patch("/:id/like", verifyToken, likePost);
 
export default router;
  • The first parameter in the router.patch() method is a dynamic path that includes a placeholder variable called "id". This variable represents the unique ID of the post that the user wants to like.
  • The second middleware function in the chain is "verifyToken", which is responsible for verifying the user's authentication and authorization to access the requested resource.
  • If the user is authenticated, the third middleware function in the chain is called, which is "likePost".
  • The purpose of the "likePost" function is to update the likes count of a specific post in the database.
  • When the user clicks the "like" button on a post, this function increments the likes count of that post by 1.
  • In a production-level app, this function might also log the user's action in the database for analytical purposes or to prevent spam.
  • For example, if we had a post with an ID of "123" and we made a PATCH request to "/123/like", the "likePost" function would update the likes count of that post in the database and return a response to the client confirming the success of the operation.

In summary, the code snippet sets up a router to listen for PATCH requests on the "/:id/like" URL path, where the "id" parameter represents the unique ID of the post that the user wants to like. The server verifies the user's authentication and authorization using the "verifyToken" middleware function, and then updates the likes count of that post in the database using the "likePost" middleware function.