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

AOP with FeathersJS

82d2b4bab91a8216032461d552fbc3a6?s=47 AbraaoAlves
September 04, 2016

AOP with FeathersJS

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

82d2b4bab91a8216032461d552fbc3a6?s=128

AbraaoAlves

September 04, 2016
Tweet

More Decks by AbraaoAlves

Other Decks in Programming

Transcript

  1. Aspect Oriented Programming with FeathersJS

  2. Why Aspect?

  3. 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
  4. @abraao4lves Software Engennier abraaoalves.github.io

  5. Inspiration: Functional Programming f(g(x))

  6. Continuation–passing style (CPS)

  7. Continuation–passing style (CPS) let createUser = validateRequest >> verifyEmail >>

    db.createUser >> smtpServer.sendEmail >> returnMessage F# Example:
  8. (CPS) with NodeJS ?

  9. 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
  10. 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
  11. And ... Aspect Oriented Programming?

  12. Aspect Oriented Programming!

  13. None
  14. Get Starter

  15. (CPS) with FeathersJS

  16. (CPS) with FeathersJS // Register the hooks app.service('users') .before({ find:

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

    [isLoggedIn, isAdmin], get: [isLoggedIn, isCurrent], create: hashPassword }) .after(removePasswords) .after({ create: sendEmail });
  18. (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 });
  19. DEMO

  20. Thanks. @abraao4lves abraaoalves.github.io