Deleting

The API endpoint to delete a user is declared in the following route.

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

router.route('/api/users/:userId').delete(userCtrl.remove)

When the Express app gets a DELETE request at '/api/users/:userId', similar to the read and update, it first loads the user by ID, and then the remove controller function is executed.

mern-skeleton/server/controllers/user.controller.js:

const remove = (req, res, next) => {  let user = req.profile  user.remove((err, deletedUser) => {    if (err) {      return res.status(400).json({        error: errorHandler.getErrorMessage(err)      })    }    deletedUser.hashed_password = undefined    deletedUser.salt = undefined    res.json(deletedUser)  })}

The remove function retrieves the user from req.profile ...

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.