How to use `pre` in a route handler - hapi.js

I need to call a method using pre in the route. I am using hapi-request . I tried using pre in the route declaration, but I have an error. What am I missing?

My initial route:

server.route({ 
    method: 'POST', 
    path: '/searchUser',  
    config: User.searchUser
})

Using Pre

server.route({ 
    method: 'POST', 
    path: '/searchUser',  
    pre: validateUser, 
    config: User.searchUser
})

Error

Error: Invalid route options (/searchUser) {
  "method": "POST",
  "path": "/searchUser",
  "config": {}
}
←[31m
[1] "pre" is not allowed←[0m   
+5
source share
3 answers

pre must be used inside the configuration object.

From route information to Hapi:

server.route({
    method: 'GET',
    path: '/',
    config: {
        pre: [
            [
                // m1 and m2 executed in parallel
                { method: pre1, assign: 'm1' },
                { method: pre2, assign: 'm2' }
            ],
            { method: pre3, assign: 'm3' },
        ],
        handler: function (request, reply) {
            return reply(request.pre.m3 + '\n');
        }
    }
});

Updated Route:

server.route({ 
    method: 'POST', 
    path: '/searchUser', 
    config: {
        handler: User.searchUser, 
        pre: [{ method: validate /* function to be called */ }]
    }
);
+4
source

The pre property for the route configuration object is not a function ( here ), it is an array of route-prerequisites

0
source

. "config" "options" Hapijs.

0

All Articles