Listing all users

The API endpoint to fetch all the users is declared in the following route.

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

router.route('/api/users').get(userCtrl.list)

When the Express app gets a GET request at '/api/users', it executes the list controller function.

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

const list = (req, res) => {
User.find((err, users) => {
if (err) {
return res.status(400).json({
error: errorHandler.getErrorMessage(err)
})
}
res.json(users)
}).select('name email updated created')
}

The list controller function finds all the users from the database, populates only the name, email, created and updated fields in the resulting user list, and then returns this list of users as JSON objects in an array to the requesting client.