node-restful icon indicating copy to clipboard operation
node-restful copied to clipboard

Before

Open estevo-de opened this issue 9 years ago • 2 comments

Hi I found the .before Method interesting. I tried to use it without success perhaps cos I am using it together with register. It would be great if there are examples how one would be able to use it.

I have checked the lib and based on comparing .before with register somewhat I think I am missing out on a parameter for .before.

Thank you. Best regards Ashar

estevo-de avatar May 14 '16 09:05 estevo-de

Hey Ashar -- you can looks at the README.md for examples on how to use before and after properly. To quote directly,

Resource.before('post', hash_password)
  .before('put', hash_password);

function hash_password(req, res, next) {
  req.body.password = hash(req.body.password);
  next();
}

// ...

Resource.after('get', function(req, res, next) {
  var tmp = res.locals.bundle.title; // Lets swap the title and year fields because we're funny!
  res.locals.bundle.title = res.locals.bundle.year;
  res.locals.bundle.year = tmp;
  next(); // Don't forget to call next!
});

Of course, the APIs for before and after are the same

baugarten avatar May 14 '16 13:05 baugarten

Hi there,

thank you for replying. I tried that as per the docs you have online but in my case it didn't work. Probably I made a mistake somewhere. I tried to use it with Cipher (Node.js - Crpyto).

I would try it again [😊]

Have a great weekend!


Von: Ben Augarten [email protected] Gesendet: Samstag, 14. Mai 2016 15:57 An: baugarten/node-restful Cc: Ashar Abdullah; Author Betreff: Re: [baugarten/node-restful] Before (#129)

Hey Ashar -- you can looks at the README.md for examples on how to use before and after properly. To quote directly,

Resource.before('post', hash_password) .before('put', hash_password);

function hash_password(req, res, next) { req.body.password = hash(req.body.password); next(); }

// ...

Resource.after('get', function(req, res, next) { var tmp = res.locals.bundle.title; // Lets swap the title and year fields because we're funny! res.locals.bundle.title = res.locals.bundle.year; res.locals.bundle.year = tmp; next(); // Don't forget to call next! });

Of course, the APIs for before and after are the same

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHubhttps://github.com/baugarten/node-restful/issues/129#issuecomment-219221735

estevo-de avatar May 14 '16 18:05 estevo-de