Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

A hook can be defined where all requests coming into HTTP server can be processed before any other logic, including internal Profound.js code.

The hook is available by defining a function named profoundjs.preRouting. You can define that function inside of the start.js file. preRouting is expected to behave as “middleware” intended for the popular Node.js HTTP server package, Express.

Function Parameters:

  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.

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

  #!/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();
  • No labels