Dynamic import of ES6 modules

Before we start modifying the router functions, we have to consider how to account for multiple models. We currently have two modules for our data model, notes-memory and notes-fs, and we'll be implementing several more by the end of this chapter. We will need a simple method to select between the model being used.

There are several possible ways to do this. For example, in a CommonJS module, it's possible to do the following:

const path  = require('path'); 
const notes = require(process.env.NOTES_MODEL  
                  ? path.join('..', process.env.NOTES_MODEL)  
                  : '../models/notes-memory'); 

This lets us set an environment variable, NOTES_MODEL, to select the module to use for the data model.

This approach does not work with the ...

Get Node.js Web Development - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.