Skip to content

Latest commit

 

History

History
73 lines (57 loc) · 1.28 KB

lesson2.md

File metadata and controls

73 lines (57 loc) · 1.28 KB

MEAN Tutorial lesson 2

Prerequisites

  • MEAN Tutorial lesson 1

Steps

Packages

Create a file db.js

module.exports = {
  url: 'mongo://localhost'
};

require the package at the top in the main script, use it and run the script:

const db = require('./db.js');
.
.
.
log(`Initializing the application. Connected to '${db.url}'`);

Move the save function to the package:

module.exports = {
  url: 'mongo://localhost',
  save: function (data, cb) {
    log('saving to db');
    // Code to save to the db will be here...
    setTimeout(function () {
      log(`Product id '${data.productId}' saved!`);
      cb('ok');
    }, 1000);
  }
};

Change the function utilization and run the script:

db.save(data, callback);

Fix the log error passing the log object as a function parameter to the package:

module.exports = function (log) {
  return {
    url: 'mongo://localhost',
    save: function (data, cb) {
      log('saving to db');
      // Code to save to the db will be here...
      setTimeout(function () {
        log(`Product id '${data.productId}' saved!`);
        cb('ok');
      }, 1000);
    }
  }
};

And passing the log in the require:

const db = require('./db.js')(log);