Raddy Website Design & Development Tutorials

Build a Notes App using NodeJs, Express, MongoDB & Passport – CRUD

By Raddy in NodeJs ·

Today we are going to create a simple Notes taking Application using Nodejs, Express, MongoDB, and Passport. For templating we will use EJS and Bootstrap.

Improvement Ideas

  • Notes Ritch Text Editor
  • Notes Encryption
  • Predictive Search
  • Better RWD
  • Add More Authentication Options
  • Flash Messages
  • Code Improvement Overall
  • Create API to be used on Front End
  • Add More UI Interactivity

Mongoose ^7.0.0 Update

Please replace the following code in auth.js

// REPLACE THIS
passport.deserializeUser(function (id, done) {
  User.findById(id, function (err, user) {
    done(err, user);
  });
});

// TO THIS
passport.deserializeUser(async (id, done) => {
  try {
    const user = await User.findById(id);
    done(null, user);
  } catch (err) {
    done(err, null);
  }
});

In dashboardController.js please replace the full Get Dashboard Route

/**
 * GET /
 * Dashboard
 */
exports.dashboard = async (req, res) => {

  let perPage = 12;
  let page = req.query.page || 1;

  const locals = {
    title: "Dashboard",
    description: "Free NodeJS Notes App.",
  };

  try {
    // Mongoose "^7.0.0 Update
    const notes = await Note.aggregate([
      { $sort: { updatedAt: -1 } },
      { $match: { user: new mongoose.Types.ObjectId(req.user.id) } },
      {
        $project: {
          title: { $substr: ["$title", 0, 30] },
          body: { $substr: ["$body", 0, 100] },
        },
      },
    ])
    .skip(perPage * page - perPage)
    .limit(perPage)
    .exec();

    const count = await Note.count();

    res.render('dashboard/index', {
      userName: req.user.firstName,
      locals,
      notes,
      layout: "../views/layouts/dashboard",
      current: page,
      pages: Math.ceil(count / perPage)
    });
   } catch (error) {
    console.log(error);
  }
};

Credit:

More Resources:

Leave a Reply

Your email address will not be published. Required fields are marked *