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

Thanos Polychronakis

January 29, 2015
Tweet

More Decks by Thanos Polychronakis

Other Decks in Technology

Transcript

  1. /** * @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
  2. 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.
  3. 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 = {};
  4. 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) {...};
  5. 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) { ... };
  6. 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
  7. 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
  8. Beware of context Beware of context Cors.prototype.use = function(req, res,

    next) { // good doSomeAsync(this._handleAsync.bind(this)); // bad doSomeAsync(this._handleAsync); };
  9. 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)); } };
  10. 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
  11. 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
  12. 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...