April 2020
Intermediate to advanced
716 pages
18h 55m
English
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 read and update, it loads the user by ID and then the remove controller function is executed.
mern-skeleton/server/controllers/user.controller.js:
const remove = async (req, res) => { try { let user = req.profile let deletedUser = await user.remove() deletedUser.hashed_password = undefined deletedUser.salt = undefined res.json(deletedUser) } catch (err) { return res.status(400).json({ error: errorHandler.getErrorMessage(err) }) }}
The remove function retrieves the user from req.profile ...
Read now
Unlock full access