User routes

The user routes defined in the user.routes.js file will use express.Router() to declare the route paths with relevant HTTP methods, and assign the corresponding controller function that should be called when these requests are received by the server.

We will keep the user routes simple, by using the following:

  • /api/users for:
    • Listing users with GET
    • Creating a new user with POST
  • /api/users/:userId for:
    • Fetching a user with GET
    • Updating a user with PUT
    • Deleting a user with DELETE

The resulting user.routes.js code will look as follows (without the auth considerations that need to be added for protected routes).

mern-skeleton/server/routes/user.routes.js:

import express from 'express'import userCtrl from '../controllers/user.controller' ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.