Tutorial
Users Routes 1

User Routes

What is User Routes?

Creating a social media platform requires careful planning and consideration of the different user routes that users will take when using your platform. Here are the three user routes that you will need to create:

  1. Retrieve a particular user's information:
  • Create an API endpoint that takes in a user ID as a parameter and returns the user's information.
  • Example: Facebook and Twitter allow users to search for and retrieve other users' profiles by typing in their names or usernames.
  1. Retrieve a user's friends list:
  • Create an API endpoint that takes in a user ID as a parameter and returns a list of their friends' information.
  • Example: Instagram and Snapchat allow users to see their friends' profiles and stories.
  1. Add or remove friends:
  • Create API endpoints that allow users to send friend requests, accept or decline friend requests, and remove friends from their friends list.
  • Example: Facebook and LinkedIn allow users to send friend requests and build their networks.

By creating these user routes, you will be able to create a social media platform that is versatile and user-friendly.

User.js Routes

  • We will add separate routes for each of the three user routes that we have identified.
├── server
│   ├── index.js
import userRoutes from "./routes/users.js";
app.use("/users", userRoutes);
  • We will create a new folder called routes and a new file called users.js inside of it.
├── server
│   ├── routes
│   │   ├── users.js
import express from "express";
import {
  getUser,
  getUserFriends,
  addRemoveFriend,
} from "../controllers/users.js";
import { verifyToken } from "../middleware/auth.js";
 
const router = express.Router();
 
/* READ */
router.get("/:id", verifyToken, getUser);
router.get("/:id/friends", verifyToken, getUserFriends);
 
/* UPDATE */
router.patch("/:id/:friendId", verifyToken, addRemoveFriend);
 
export default router;

Importing Modules and Middleware

The next step is to import the necessary modules and middleware at the top of the users.js file. We will need the following:

  • The express module to create a router object that we can use to define our routes.
  • The getUser, getUserFriends, and addRemoveFriend functions from our users.js controller file. These functions will handle the logic for each route.
  • The verifyToken function from our auth.js middleware file. This function will authenticate users before they can access our routes.
import express from "express";
import {
  getUser,
  getUserFriends,
  addRemoveFriend,
} from "../controllers/users.js";
import { verifyToken } from "../middleware/auth.js";

Creating a Router Object

The next step is to create a router object using the express.Router() method. This object will allow us to define our routes using methods like .get(), .post(), .patch(), etc.

const router = express.Router();

Defining User Routes

Now we can define our user routes using the router object. We have two READ routes and one UPDATE route:

  • GET /users/:id: This route gets a user by their ID.
  • GET /users/:id/friends: This route gets a user’s friends by their ID.
  • PATCH /users/:id/:friendId: This route adds or removes a friend from a user’s friend list.

For each route, we need to specify three things:

  • The HTTP method (GET or PATCH).
  • The URL path (with parameters if needed).
  • The middleware functions (verifyToken and one of the controller functions).
/* READ */
router.get("/:id", verifyToken, getUser);
router.get("/:id/friends", verifyToken, getUserFriends);
 
/* UPDATE */
router.patch("/:id/:friendId", verifyToken, addRemoveFriend);

Exporting Router Object

The final step is to export our router object using the default export syntax. This way, we can import it in our index.js file and mount it on the /users route.

export default router;

Example Using Social Media Platform

Let’s now use SocialBook as an example to show how these user routes might work in practice.

Imagine we have two users: Alice with ID 1234 and Bob with ID 5678. Alice wants to view Bob’s profile page. She would send a GET request to this URL:

GET /users/5678

This request would be handled by our router object in users.js file. It would first call verifyToken function to check if Alice has a valid token in her request header. If she does, it would then call getUser function with Bob’s ID as parameter. This function would fetch Bob’s information from database and return it as JSON response.