customHttpHeaders

customHttpHeaders

customHttpHeaders is available in Profound.js versions after 7.4.0.

Description

Specify a function, or a string containing an absolute path to a module that exports a function, which executes before the server responds to HTTP requests for a session page such as a Rich Display session.

Through customHttpHeaders the server can be configured to override the default HTTP response headers, which may use stricter security than the application needs. For example, by default HTTP headers are sent that prevent a Rich Display session from displaying in an iframe; to load the page inside an iframe, the Content-Security-Policy and X-Frame-Options headers may need to be changed or removed.

Function Parameters

  • request - The HTTP request object, which can be read to change headers conditionally;

  • response - The HTTP response object, before it is sent to the client.

The module is loaded at server startup and cached like any other Node.js module, so the instance must be restarted after the module is changed.

Example 1: Define the Function Inline

Remove the two headers that stop a Rich Display session from being embedded, so the session can be shown inside an iframe on another page:

config.js
// Remove headers that prevent Rich Display sessions from loading inside of an iframe. customHttpHeaders: function(request, response) { response.removeHeader("X-Frame-Options"); response.removeHeader("Content-Security-Policy"); }

Example 2: Point at a Separate Module

Keeping the logic in its own module is easier to maintain when the header rules are more involved. In config.js:

config.js
customHttpHeaders: path.join("c:", "profoundjs", "customHttpHeaders.js")

Content of customHttpHeaders.js. Rather than removing the policy entirely, this narrows it so that only the Profound.js server itself may frame the page:

customHttpHeaders.js
module.exports = function(request, response) { // Allow pages to be inside of iframes if the parent iframe is the same PJS server. response.setHeader("Content-Security-Policy", "frame-ancestors " + profound.settings.host); };

For information on how to modify this setting, see Configuration File.