preRouting
The preRouting configuration option 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. The function executes when the server receives any HTTP request, before any other logic acts on the request – including internal Profound.js code.
preRouting is expected to behave as “middleware” for the popular Node.js HTTP server package, Express.
Function Parameters
request - The HTTP request object, which can be read to decide what to do;
response - The HTTP response object, before it is sent to the client;
next - A function that must be called if other logic should handle the request. If
next()is not called, no other internal Profound.js code issues a response to the client.
Forgetting to call next() leaves the request hanging with no response at all. Every code path through the function must either call next() or send its own response.
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
config.js
// Send an additional header that allows content to be inside of iframes when the parent iframe is the PJS server.
preRouting: function(request, response, next) {
response.setHeader("Content-Security-Policy", "frame-ancestors " + profound.settings.host);
next(); // Mandatory: let other logic process the request so that a response will eventually be issued.
}Example 2: Point at a Separate Module
In config.js:
config.js
preRouting: path.join("var", "profoundjs", "preRouting.js")Content of preRouting.js:
preRouting.js
module.exports = function(request, response, next) {
response.setHeader("Content-Security-Policy", "frame-ancestors " + profound.settings.host);
// Mandatory: without next(), Profound.js never responds to the request.
next();
};To change response headers only for session pages rather than every request, see customHttpHeaders.
For information on how to modify this setting, see Configuration File.