Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Authoring Node.js Modules

Authoring Node.js Modules

How to author modules in Node.js

Cbb9cafe03e785d80a9aa973c5e43c44?s=128

Thanos Polychronakis

January 29, 2015
Tweet

Transcript

  1. Modules in node.js Modules in node.js

  2. What do we mean by modules? What do we mean

    by modules? Files!
  3. Disclamer Disclamer Opinionated Best Practices Opinionated Best Practices ahead ahead

  4. The Module Layout The Module Layout

  5. /** * @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
  6. The Dependencies 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.
  7. The Module Types The Module Types

  8. Constructors Constructors Objects Objects Only 2 types of Modules Only

    2 types of Modules
  9. Module Types Module Types Constructors Constructors var Cors = module.exports

    = function(){ Middleware.apply(this, arguments); }; util.inherits(Cors, Middleware); Objects Objects var Cors = module.exports = {};
  10. Module Types Module Types Constructor 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) {...};
  11. Module Types Module Types Module 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) { ... };
  12. If you want state --> Ctor If you want inheritance

    --> Ctor Helpers / Util --> Obj Enums / Dicts --> Obj Config Files --> Obj When to use which When to use which It's all about the state It's all about the state
  13. Module Inheritance Module Inheritance The classical way The classical way

  14. Module Inheritance Module Inheritance The Classical way 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
  15. Beware of context Beware of context Cors.prototype.use = function(req, res,

    next) { // good doSomeAsync(this._handleAsync.bind(this)); // bad doSomeAsync(this._handleAsync); };
  16. Beware of performance 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)); } };
  17. Shameless Plug Time Shameless Plug Time CIP! CIP!

  18. CIP CIP Classical Inheritance Pattern at its Best 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
  19. CIP CIP Classical Inheritance Pattern at its Best 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
  20. CIP CIP Classical Inheritance Pattern at its Best 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...
  21. CIP CIP /thanpolas/cip /thanpolas/cip

  22. Thank you (here is where you applaud) Thanasis Polychronakis @thanpolas

  23. Questions? Thanasis Polychronakis @thanpolas