In the Node.js backend application, we are going make available endpoints, defined in our Express application routes, related to managing conversation threads. Also, there should be a way to get the message history from a specific thread.
We are going to add the necessary business logic to manage our threads in a new controller file, called app/controllers/thread.js
, by following these steps:
Add the required modules:
'use strict'; const mongoose = require('mongoose'); const Thread = mongoose.model('Thread');
Export the module's methods:
module.exports.allByUser = allThreadsByUser; module.exports.find = findThread; module.exports.open = openThread; module.exports.findById = findThreadById;
Find all the threads for a specific user:
function allThreadsByUser(req, res, next) { Thread .find({ participants: req.user._id }) .populate('participants') .exec((err, threads) => { if (err) { return next(err); } req.resources.threads = threads...