November 2018
Beginner
132 pages
2h 57m
English
The index route will retrieve all of the contacts from the database and send them back to the client in a JSON object. We can use the mongoose .find() method to retrieve all of the entries of a model from the database.
Insert the following code into the ContactController.js file:
// ./controllers/ContactController.jsconst Contact = require('../models/Contact');module.exports = { async index(ctx) { const contacts = await Contact.find(); ctx.body = { status: 'success', data: contacts }; }};
Next, we head over to the ./middleware/router.js file, in order to add the corresponding route, as follows:
// ./middleware/router.js// ...const contactController = require('../controllers/ContactController');router .get('/', async ...Read now
Unlock full access