Call function as a route-handler?

Himmators

I'm building a REST-api and have rendering function that looks like this:

render = (req, res) => {
    var output = {},
        async = [],
        detailed;

    if(req.user){
        detailed = req.user.obj.detailed();
        async.push(detailed);
    }
    if(Object.keys(req.query).length){
        output.params = req.query;
    }
    if(req.validationErrors().length > 0 ){
        output.errors = req.validationErrors();
    }
    sequelize.Promise.all(async).then((response) => {
        if(response.length > 0){
            output.user = response[0];
        }
        res.json(output);
    });
};

It grabs stuff and ouputs it like a JSON. It's usually run as the last function in any request:

app.all('/user/me', auth, render);

In the app I also validate input data at different stages. If the input fails, I want to go straight to the rendering. In order to avoid passing around req/res all over the place, I would like to somehow go to that routing, rather than calling the function, like I am now, is that possible? I can't use next, because sometimes there are more than one function left.

    validate = (tests) =>{
        req.assert(tests);
        //Go to render if validation fails.

        //req,res
        if (req.validationErrors().length) {
            return render(req, res);
        }else{
            return true;
        }
    };
leetibbett

You could hook up some Error-handling middleware that runs your standard render method. Then your route validations would need to generate an error.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

RxJava unable to call handler for a route

From Dev

Angular Route - Call function on load

From Dev

How to call a route by its name from inside a handler?

From Dev

Call Swift function with completion handler in objective c

From Dev

If A Radio Button is Checked Call Function Handler

From Dev

Call function inside event handler oop

From Dev

Call event with function handler + parameter in jQuery

From Dev

Call a method inside an event handler function

From Dev

React - call handler from var in render function

From Dev

Call Swift function with completion handler in objective c

From Dev

AngularJS call scope function on route change

From Dev

EmberJS - Call a function whenever route changed

From Dev

when should a function return in hapijs route call

From Dev

call function object from route in express js

From Dev

when should a function return in hapijs route call

From Dev

Is it possible to call a block completion handler from another function in iOS?

From Dev

node.js request handler wont call a function

From Dev

Why signal function is not able to call my signal Handler?

From Dev

Can I call a PHP function in a JavaScript click handler?

From Dev

How to call public function from event handler in JavaScript?

From Dev

`this` is undefined in expressJS route handler

From Dev

why (okfn) function is reqiuerd in the handler function of IP_PRE_ROUTE hook

From Dev

Call app.js function in route file Node.js

From Dev

How to call global function on route change within Durandal?

From Dev

Aurelia, call click function before route-href

From Dev

Phoenix JSON API - configure route to call correct function in controller

From Dev

How can I wrap every every express route handler with another function

From Dev

How do I handle errors in a function called from a route handler in Express, NodeJS?

From Dev

How we can call a public function inside public function using event handler in javascript

Related Related

  1. 1

    RxJava unable to call handler for a route

  2. 2

    Angular Route - Call function on load

  3. 3

    How to call a route by its name from inside a handler?

  4. 4

    Call Swift function with completion handler in objective c

  5. 5

    If A Radio Button is Checked Call Function Handler

  6. 6

    Call function inside event handler oop

  7. 7

    Call event with function handler + parameter in jQuery

  8. 8

    Call a method inside an event handler function

  9. 9

    React - call handler from var in render function

  10. 10

    Call Swift function with completion handler in objective c

  11. 11

    AngularJS call scope function on route change

  12. 12

    EmberJS - Call a function whenever route changed

  13. 13

    when should a function return in hapijs route call

  14. 14

    call function object from route in express js

  15. 15

    when should a function return in hapijs route call

  16. 16

    Is it possible to call a block completion handler from another function in iOS?

  17. 17

    node.js request handler wont call a function

  18. 18

    Why signal function is not able to call my signal Handler?

  19. 19

    Can I call a PHP function in a JavaScript click handler?

  20. 20

    How to call public function from event handler in JavaScript?

  21. 21

    `this` is undefined in expressJS route handler

  22. 22

    why (okfn) function is reqiuerd in the handler function of IP_PRE_ROUTE hook

  23. 23

    Call app.js function in route file Node.js

  24. 24

    How to call global function on route change within Durandal?

  25. 25

    Aurelia, call click function before route-href

  26. 26

    Phoenix JSON API - configure route to call correct function in controller

  27. 27

    How can I wrap every every express route handler with another function

  28. 28

    How do I handle errors in a function called from a route handler in Express, NodeJS?

  29. 29

    How we can call a public function inside public function using event handler in javascript

HotTag

Archive