middleware/routing

module:middleware/routing

Description:
  • Config based rewriting and redirection middleware, for redirection 30X code must prefix the url

Source:
Examples
// Enable dynamic routing, every request will be checked against current config
middleware-routing-enable = true

middleware-routing-/app/* = /index.html

middleware-routing-/old/endpoint/ = /new/@PATH2@

middleware-routing-/login/* = 302/login.html?path=@PATH@

// Enable fixed routing, only the above configured routes are checked
middleware-routing-enable = fixed

For manual routing here is an example how to do it in the code

api.app.get("/path/", (context, next) => {
    context.setUrl("/new/path");
    next("restart");
});

Methods

(static) handle(context, next)

Description:
  • Check if the current request must be re-routed or redirected to another endpoint, uses the global config, formatting using RequestContext#format

Source:
Parameters:
Name Type Description
context RequestContext
next function()
Example
const { api, middleware } = require("backendjs");
const { routing } = middleware;

api.app.post("*", routing)

api.app.post("/acct/*", { location: "/account/@PATH2@", handle: routing.handle })