Middleware routes:after phase does not work

40 views
Skip to first unread message

James Cooke

unread,
May 28, 2015, 4:59:19 AM5/28/15
to loopb...@googlegroups.com
I've tried a couple of ways to tie my middleware to the routes:after phase and am having no success.

In server.js I have tried:
app.middleware("routes:after",[],function(req,res,next) {
 
console.log("here");
 
next();
})

I have also tried:
middleware.json
"routes:after": {
 
"./middleware/test": {}
},
middleware/test.js
module.exports = function() {
 
return function tracker(req, res, next) {
   
console.log('Middleware triggered on %s.', req.url);
   
next();
 
};
};

Neither trigger a console log however if I change routes:after to routes:before they do work. Also triggering off other phases works. Any ideas?

Raymond Feng

unread,
May 28, 2015, 4:17:45 PM5/28/15
to loopb...@googlegroups.com
What URL do you hit? Please note that /api/<model> doesn’t proceed to other middleware as it’s handled by remote methods on models. 

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Cooke

unread,
May 29, 2015, 4:53:55 PM5/29/15
to loopb...@googlegroups.com
Ah right so you can only use the middleware with non /api/* requests. That's quite annoying.

I have actually worked around this with a boot script which uses app.remotes().after() but middleware seemed to be a more generic method to accomplish this. 

Thanks for replying - what is the best way to update the documentation as I found this not obvious - can I submit a pull request somewhere?

James

Raymond Feng

unread,
May 29, 2015, 5:24:33 PM5/29/15
to loopb...@googlegroups.com
Remote hooks can be used to intercept the requests to /api/… before it’s handed over the the model method implementation.

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

James Cooke

unread,
May 29, 2015, 5:29:58 PM5/29/15
to loopb...@googlegroups.com
I'm looking to modify the response to add HAL type links to it. Is there a better hook I can use as the "access" hook comes before the model I need something after the model.

Raymond Feng

unread,
May 29, 2015, 5:31:05 PM5/29/15
to loopb...@googlegroups.com
You can do it with afterRemote hooks.

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

Reply all
Reply to author
Forward
0 new messages