Modules in node.js

What do we mean by modules?

Files!

Disclamer

Opinionated Best Practices ahead

The Module Layout

/**
 * @fileOverview Cors Middleware.
 */
var util = require('util');
var fs = require('fs');

var Promise = require('bluebird');
var _ = require('lodash');

var Middleware = require('./middleware');
var helpers = require('../util/helpers');

/**
 * The Cors Middleware.
 *
 */
var Cors = module.exports = {};

/**
 * CORS Middleware
 *
 * @param {Object} req The request Object.
 * @param {Object} res The response Object.
 * @param {Function(Error=)} next pass ctrl.
 */
Cors.use = function(req, res, next) { ... };

Module Description

Dependencies

System Deps

NPM Deps

Project Deps

Export Statement

Method Definition

DocBlocks for Args

Function Expression

The Dependencies

@thanpolas

  1. System Dependencies
  2. NPM Dependencies
  3. Project Dependencies
/**
 * @fileOverview Cors Middleware.
 */
var util = require('util');
var fs = require('fs');

var Promise = require('bluebird');
var _ = require('lodash');

var Middleware = require('./middleware');
var helpers = require('../util/helpers');

1.

2.

3.

The Module Types

Constructors

Objects

Only 2 types of Modules

Module Types

Constructors

var Cors = module.exports = function(){
  Middleware.apply(this, arguments);
};
util.inherits(Cors, Middleware);

Objects

var Cors = module.exports = {};

Module Types

Constructor

/**
 * The Cors Middleware.
 *
 * @contructor
 * @extends {cc.Middleware}
 */
var Cors = module.exports = function(){
  Middleware.apply(this, arguments);
};
util.inherits(Cors, Middleware);

/**
 * CORS Middleware
 *
 * @param {Object} req The request Object.
 * @param {Object} res The response Object.
 * @param {Function(Error=)} next pass ctrl.
 */
Cors.prototype.allowCrossDomain = function(req, res, next) {...};

Module Types

Module

/**
 * The Cors Middleware.
 *
 */
var Cors = module.exports = {};

/**
 * CORS Middleware
 *
 * @param {Object} req The request Object.
 * @param {Object} res The response Object.
 * @param {Function(Error=)} next pass ctrl.
 */
Cors.use = function(req, res, next) { ... };
  • If you want state --> Ctor
  • If you want inheritance --> Ctor
  • Helpers / Util --> Obj
  • Enums / Dicts --> Obj
  • Config Files --> Obj

When to use which

It's all about the state

Module Inheritance

The classical way

Module Inheritance

The Classical way

var util = require('util');
var EventEmitter = require('events').EventEmitter;

var MiddlewareBase = module.exports = function() {
  EventEmitter.apply(this, arguments);

  this.foo = 1;
};
util.inherits(MiddlewareBase, EventEmitter);
middleware-base.midd.js
var util = require('util');
var MiddleWareBase = require('./middleware-base.midd');

var Cors = module.exports = function() {
  MiddlewareBase.apply(this, arguments);

  console.log(this.foo); // 1
};
util.inherits(Cors, MiddlewareBase);
cors.midd.js

Beware of context

Cors.prototype.use = function(req, res, next) {
  // good
  doSomeAsync(this._handleAsync.bind(this));

  // bad
  doSomeAsync(this._handleAsync);
};

Beware of performance

Cors.prototype.use = function(req, res, next) {
  while(true) { // highly repetitive operation
    // good
    var self = this;
    doSomeAsync(function(result) {
      self._handleAsync(result);
    });

    // bad
    doSomeAsync(this._handleAsync.bind(this));
  }
};

Shameless Plug Time

CIP!

CIP

Classical Inheritance Pattern at its Best

var util = require('util');
var EventEmitter = require('events').EventEmitter;

var MiddlewareBase = module.exports = function() {
  EventEmitter.apply(this, arguments);

  this.foo = 1;
};
util.inherits(MiddlewareBase, EventEmitter);

Base Constructors vanilla

var EventEmitter = require('events').EventEmitter;
var cip = require('cip');

var CeventEmitter = cip.cast(EventEmitter);

module.exports = CeventEmitter.extend();

Base Constructors using CIP

CIP

Classical Inheritance Pattern at its Best

var util = require('util');
var MiddleWareBase = require('./middleware-base.midd');

var Cors = module.exports = function() {
  MiddlewareBase.apply(this, arguments);

  console.log(this.foo); // 1
};
util.inherits(Cors, MiddlewareBase);

Child Constructors vanilla

var MiddlewareBase = require('./middleware-base.midd');

var Cors = module.exports = MiddlewareBase.extend(function () {
  /* Ctor */
});

Child Constructors using CIP

CIP

Classical Inheritance Pattern at its Best

var MiddlewareBase = require('./middleware-base.midd');
var MiddlewareTraits = require('./middleware-traits.midd');

var Cors = module.exports = MiddlewareBase.extend(function () {
  /* Ctor */
});
Cors.mixin(MiddlewareTraits);

Mixins!

var Cors = require('./cors.midd');

var SuperCors = module.exports = Cors.extend(function () {
  /* Ctor */
});

Inheritance Goes on...

CIP

/thanpolas/cip

Thank you

(here is where you applaud)

Thanasis Polychronakis

@thanpolas

Questions?

Thanasis Polychronakis

@thanpolas

Modules in Node.js

By thanpolas

Modules in Node.js

How to author modules in node.js

  • 453
Loading comments...

More from thanpolas