We are going to implement our first backend module for our application. This module will handle everything that is related to a company.
We are going to add a simple but interesting functionality to the company model, which will create a so-called slug from the company name. A slug
, in our context, is generated from the name of the company to be accepted as a valid URL. It will be used to reference the company in a meaningful way. For example, if we have a company named Your Awesome Company
in the system, the resulting slug will be your-awesome-company
.
To generate the slug, we'll implement a simple helper function so that we can reuse it later if necessary. Create a file called app/helpers/common.js
and add the following lines of code:
'use strict'; module.exports.createSlug = createSlug; function createSlug(value) { return value .toLowerCase() .replace(/[^\w\s]+/g,'') .trim() .replace(/[\s]+/g,'-'); }
Now that we have the helper
...