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

AOP with FeathersJS

AbraaoAlves
September 04, 2016

AOP with FeathersJS

Show a powerful and flexible way to create API services in NodeJS.

AbraaoAlves

September 04, 2016
Tweet

More Decks by AbraaoAlves

Other Decks in Programming

Transcript

  1. Why Aspect ... var express = require('express'); var app =

    express(); app.update('/', function (req, res, next) { if (isLoggedln(req) && isValidate(req)) { getUser(req.params.userId).then(function (user) { if (isAdmin(user) && isCurrent(user)) { updateInfo(req).then(function (info) { if (info.emailme) { sendEmail(user.email, info) } removePassword(user); res.send(info); }) } }); } }); because systems grow
  2. Continuation–passing style (CPS) let createUser = validateRequest >> verifyEmail >>

    db.createUser >> smtpServer.sendEmail >> returnMessage F# Example:
  3. var express = require('express'); var bodyParser = require('body-parser'); var app

    = express(); app .use(bodyParser.json()) .use(function (req, res, next) { getUser(req.params.userId).then(function(user){ req.body.username = user.name; next() }) }) .use(function (req, res) { res.json(req.body || {message:'No content'}) }) (CPS) with NodeJS! Express - Middlewares
  4. var express = require('express'); var bodyParser = require('body-parser'); var app

    = express(); app .use(bodyParser.json()) .use(function (req, res, next) { getUser(req.params.userId).then(function(user){ req.body.username = user.name; next() }) }) .use(function (req, res) { res.json(req.body || {message:'No content'}) }) (CPS) with NodeJS! Express - Middlewares
  5. (CPS) with FeathersJS // Register the hooks app.service('users') .before({ find:

    [isLoggedIn, isAdmin], get: [isLoggedIn, isCurrent], create: hashPassword }) .after(removePasswords) .after({ create: sendEmail });
  6. (CPS) with FeathersJS // Register the hooks app.service('users') .before({ find:

    [isLoggedIn, isAdmin], get: [isLoggedIn, isCurrent], create: hashPassword }) .after(removePasswords) .after({ create: sendEmail });
  7. (CPS) with FeathersJS // Register the hooks app.service('users') .before({ find:

    [isLoggedIn, isAdmin], get: [isLoggedIn, isCurrent], create: hashPassword }) // To all methods .after(removePasswords) .after({ create: sendEmail });