Problem

We were recently commissioned to create a content management system (CMS) for a London company. Having only ever come across a CMS as passive end users, we were quite understandably unsure how to proceed. We had already come across other service providers, including the one offered by Netlify, described as follows:

A CMS for static site generators. Give users a simple way to edit and add content to any site built with a static site generator.

Netlify

In the end, we rallied and decided to forge ahead with the project. Here’s a brief overview of how we did it.

First step

We knew we would need to implement our CMS via a separate back end. In this instance, we settled on Node.js because of the wide variety of hosting options and the attractive option of using MongoDB, something we find less cumbersome than SQL.

Next, we created our database models, including models for Tour, Type and Attendance.

Server-side code

Below is a small sample of our server-side code for the CMS we created. We allowed appropriately authenticated users to edit content based on their role in the company.

exports.updateTour = (req, res) => {
  const validationResult = validateTourForm(req.body);
  if (!validationResult.success) {
    return res.status(400).json({
      success: false,
      message: validationResult.message,
      errors: validationResult.errors
    });
  }
  const dateFormat = "DD/MM/YYYY";
  const tourData = {
    date: moment(req.body.date.trim(), dateFormat).format("DD/MM/YYYY"),
    startTime: req.body.startTime.trim(),
    endTime: req.body.endTime.trim(),
    user_id: mongoose.Types.ObjectId(req.body.user_id.trim()),
    type_id: mongoose.Types.ObjectId(req.body.type_id.trim()),
    location_id: mongoose.Types.ObjectId(req.body.location_id.trim()),
    approved: true
  }
  Tour.findOneAndUpdate({ _id: req.params.id }, tourData, {
    new: true
  })
  .then(tour => {
    res.status(200).json({
      tour,
      message: "This tour has been updated successfully."
    })
  });
};

Notes

We made sure to return an appropriate message to the end user based on the status code of the request made to the server. This allowed us to deliver a smooth user experience with minimal uncertainty and hesitation from the people using our application.


Did you find this helpful? Do you have anything to add? Feel free to reach out on Twitter