middleware/routing

module:middleware/routing

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

    Global mode

    Enable via middleware-routing-enable = true to dynamically check every request path, this allows to add more routes to the config without restarting

    middleware-routing-enable = true
    
    middleware-routing-/app/* = /index.html
    
    middleware-routing-/old/endpoint/ = /new/@PATH2@
    
    middleware-routing-/login/* = 302/login.html?path=@PATH@
    

    Fixed config mode

    To enable just what is in the config on start and ignore new routes, modifying existing routes is still supported

    middleware-csrf-enable = fixed
    
Source:
Example

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 })