hapi icon indicating copy to clipboard operation
hapi copied to clipboard

request.app not set correctly?

Open alumowa opened this issue 11 years ago • 6 comments

Running on 8.8.0 -The app config obj in the route option is not being exposed through request.app in the handler.

I may have missed it going through the breaking changes list. Was this feature removed? I'm referencing an older example here: https://github.com/hapijs/hapi/issues/1500

var Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ port: 3000 });

server.route({
    method: 'GET',
    path: '/',
    config: {
        app: {
            foo: 'bar'
        }
    },
    handler: function (request, reply) {
        reply('Hello ' + request.app.foo); //Returns Hello undefined - request.app = {}
    }
});

server.start(function () {
    console.log('Server running at:', server.info.uri);
});

alumowa avatar Jul 17 '15 21:07 alumowa

You should be able to find it exposed in the route settings: request.route.settings.app. request.app also exists, but as far as I know would generally be used/set in server extensions, prerequisites, or the handler.

devinivy avatar Jul 18 '15 17:07 devinivy

@devinivy thanks for the clarification!

alumowa avatar Jul 18 '15 19:07 alumowa

:+1: sure thing!

devinivy avatar Jul 19 '15 02:07 devinivy

I think many people has this confusion and I found myself doing this a lot:

var something = request.route.settings.app.something || request.app.something;

Have you considered initializing request.app with the values in request.route.settings.app?

rubennorte avatar Jan 15 '17 14:01 rubennorte

One complication there is that during onRequest, request.app should exist, but the request isn't routed yet, so there's no way to know which route settings to dip into. If you'd like to initialize request.app in that way, you can do so by adding an onPreAuth request extension (see server.ext()) that merges request.route.settings.app into request.app.

devinivy avatar Jan 15 '17 16:01 devinivy

That's the way I'm already doing it, but I thought it'd be a nice thing to have in the framework. Same with a plugin I have to use request.app as default values for the templates. I understand though this complication may prevent it from being implemented in the framework.

Thanks!

rubennorte avatar Jan 15 '17 18:01 rubennorte