request.app not set correctly?
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);
});
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 thanks for the clarification!
:+1: sure thing!
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?
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.
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!