Step 4

The routes/note.js file contains the following statement which generates sample data. This sample data generation is not a responsibility of a route handler. We need to refactor and extract this segment into its own file (module).

Create a folder called data and add a file notes.js to it, with he following content:

function addSampleNotes(notes) {
  notes.create("Sample 1", "Author 1");
  notes.create("Sample 2", "Author 2");
  notes.create("Sample 3", "Author 2");
  notes.create("Sample 4", "Author 1");
}

module.exports = { addSampleNotes };

Update the routes/note.js file:

const express = require("express");
+ const { addSampleNotes } = require("../data/notes.js");
const router = express.Router();

const notes = new NoteDao();
- notes.create("Sample 1", "Author 1");
- notes.create("Sample 2", "Author 2");
- notes.create("Sample 3", "Author 2");
- notes.create("Sample 4", "Author 1");
+ addSampleNotes(notes);

Save the files and run the API requests in Postman to ensure this refactoring has not broken our API's behavior.

Now stop the API server! We are going to install a package that assists us with generating more realistic fake data! Run the following command in the terminal.

npm install faker

The faker package can generate fake users, images, content, etc.

Update data/notes.js:

const faker = require("faker");

const NUM_SAMPLES = 4;

function addSampleNotes(notes) {
  for (let i = 0; i < NUM_SAMPLES; i++) {
    notes.create(faker.lorem.paragraphs(), faker.name.findName());
  }
}

module.exports = { addSampleNotes };

Run the API server and run the API requests in Postman to ensure this refactoring has not broken our API's behavior. You may need to update some of the requests; for instance, the author name "Author 2" is not among the sample data anymore so a request to retrieve notes written by this author will return an empty array object.

Note the drawback of using a package like faker is that the randomness involved in generating fake data makes it difficult to test the API.

We can also use faker's random "unique ID" generator to assign unique IDs to notes; it will be more realistic to what a database does compared to our auto-incrementing ID feature currently in place in NoteDao.

Update the NoteDao file as follows:

  1. import faker

    const faker = require("faker");
    
  2. Use it to assign random unique IDs:

    - note._id = this.nextID();
    + note._id = faker.random.uuid();
    
  3. You don't need the uniqueID function, so delete it!

  4. You don't need this statement in the constructor, so delete it:

    - this.nextID = uniqueID();
    

Open the routes/notes.js file and remove the following casting (since IDs are not integers anymore)

- const id = Number.parseInt(req.params.id);
+ const id = req.params.id;

Save the files and run the API requests in Postman to ensure this refactoring has not broken our API's behavior. You will need to update many of the requests; for instance, every request that contained a "note id" request parameter needs to be updated as we no longer have integer IDs.