2. Setting Expectations
๏ต Target Audience
๏ต Web Developers
๏ต Web Designers
๏ต Developers with experience using other service side languages such as PHP,
ASP.NET, Python, Ruby etc.
3. Module Overview
๏ต Exports a Namespace
๏ต Exports a Function
๏ต Exports a Higher Order Function
๏ต Exports a Constructor
๏ต Exports a Singleton
๏ต Extends a Global Object
๏ต Applies a Monkey Patch
4. Module Design Patterns
๏ต Modules are still JavaScript
๏ต Possess many of the challenges of pre-ES6 Javascript
๏ต Design Patterns to help with
๏ต Encapsulation
๏ต Stable Interface
6. Exports a Namespace
๏ต Module returns an object
๏ต Object contains properties and functions
๏ต Calling code can refer to properties and execute functions
7. Simply return an object with whatever properties and functions the
calling code should have access to
//private module stuff can happen here
//then return an object
module.exports = {
property1: 'value',
property2: 'value',
function1: function(){ โฆ }
function2: function(){ โฆ }
}
8. Core 'fs' Node module returns an object
readFile and ReadStream are functions of the returned object
Both are accessible from this calling function
Analogous to static classes and members in other languages
var fs = require('fs');
fs.readFile('./file.txt', function(err, data) {
console.log("readFile contents: '%s'", data);
});
new fs.ReadStream('./file.txt').on('data', function(data) {
console.log("ReadStream contents: '%s'", data);
});
10. Exports a Function
๏ต Factory function
๏ต Gives you instancing since all variables are encased in a closure
๏ต Closure is run for each require()
๏ต Revealing Module Pattern you may have used in client side JavaScript
๏ต ProTip
๏ต If you donโt need instancing โ export a namespace
๏ต If you need instancing โ export a constructor
14. Exports a Higher Order Function
๏ต Like the former, but also receives a function that affects the behavior of the function it returns
๏ต Express middleware is a great example - functions are provided and the middleware function is returned
15. Chaining is possible with the app object because the middleware
functions each return it.
var app = require('express')();
app.use('/route1', function(res, req, next) {
//do something
next();
});
app.use('/route2', function(res, req, next) {
//do something
next();
});
17. Exports a Constructor
๏ต Constructor function creates instance
๏ต Prototype used to define behavior
๏ต Caller creates instance with new keyword
๏ต Multi instance
20. Exports a Singleton
๏ต Instantiates object before returning it
๏ต Causes all calling modules to share a single object instance
21. When an object is instantiated before it's returned, it acts as a
singleton.
function Something() {
โฆ
}
module.exports = new Something();
22. Mongoose is one example of a good use of the singleton pattern
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var Cat = mongoose.model('Cat', { name: String });
var kitty = new Cat({ name: 'Zildjian' });
kitty.save(function (err) {
if (err) // ...
console.log('meow');
});
24. Extends a Global Object
๏ต Modifies an existing type i.e. String
๏ต Doesnโt have to export anything
๏ต Makes for nice calling syntax but can be difficult to track down source in large project
๏ต Frowned upon in open source packages
26. Applies a Monkey Patch
๏ต A monkey patch is the dynamic modification of a class or object at runtime to fix a bug in existing code
๏ต Similar to previous pattern (extends a global object) but affects cached node modules
27. The winston logger defaults all profiling statements to the info level
and you canโt change
A quick monkey patch allows you to log data out as any log level you
want and replace the built in functionality without forking the code
base
winston.Logger.prototype.profile = function(id) {
// Altered behvaior or winston loggers profile function
};
28. Summary
๏ต Exports a Namespace
๏ต Exports a Function
๏ต Exports a Higher Order Function
๏ต Exports a Constructor
๏ต Exports a Singleton
๏ต Extends a Global Object
๏ต Applies a Monkey Patch
30. What is Express?
๏ต Express is a minimal, open source and flexible node.js web app framework designed to make developing
websites, web apps and APIs much easier.
31. Why use Express?
๏ต Express helps you respond to requests with route support so that you may
write responses to specific URLs.
๏ต Supports multiple templating engines to simplify generating HTML.
35. Explanation of Routes
๏ต A router maps HTTP requests to a callback.
๏ต HTTP requests can be sent as GET/POST/PUT/DELETE, etc.
๏ต URLs describe the location targeted.
๏ต Node helps you map a HTTP GET request like:
๏ต http://localhost:8888/index
๏ต To a request handler (callback)
app.get('/index', function (req, res) {});
36. Creating a Simple Express Application
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.json({message:'hooray! welcome to our api!'});
});
app.listen(process.env.PORT || 8080);
40. Building a RESTful API for Dogs
Resource GET PUT POST DELETE
Collection URI, such
as
http://api.example
.com/v1/dogs/
List all
the
dogs
Replace all the dogs
with a new
collection of dogs.
Create a new
dog in the
collection.
Delete the
entire dog
collection.
Element URI, such
as
http://api.example
.com/v1/dog/1
Get a
specifi
c dog.
Replace a dog in the
collection with
another dog.
Not used.
Delete the dog
from the
collection.