Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. request – a Node.js HTTPRequest object containing HTTP request parameters defined as properties.

  2. response – a Node.js HTTPResponse object, where the response headers and body may be written to.

  3. next – a callback function. “next” should be called after all logic in the function finishes if other logic is expected to handle the request. If the request should stop being processed at the preRouting function, then the “next” function should not be called.

The preRouting function can be setup through the config.js file: preRouting

preRouting can also be setup in the start.js file.

Example use of preRouting in a start.js file for simply echoing a custom request header back to the client in the response:

Code Block
  #!/usr/bin/env node
async function startPJS() {
  const profoundjs = require("./index.js");

  profound.preRouting = function(request, response, next) {
    // Hypothetical use of ensuring that some header sent from the client gets sent back to the client.
    const custheader = request.headers["x-custom-header"];
    if (typeof custheader === "string") {
      response.setHeader("x-custom-header", custheader);
    }
    next();
  };

  await profoundjs.applyConfig();
  await profoundjs.server.listen();
  const express = profoundjs.server.express;
  const app = profoundjs.server.app;

  app.use(express.json());
}
startPJS();

The profound.preRouting hook is available in Profound.js versions 7.0.0 and newer.