middleware/static

module:middleware/static

Description:
  • Static middleware - Handles serving static files

    Routing globally using configured paths to static assets listed in the app.path.web property, this will include

    all packages loaded via app-import config parameter.

    By default local folder "web" is used as the static root relative to the app.home.

    The backendjs "web" folder is also enable by default to serve Alpine and Bootstrap bundles.

    Config

    // Additional public folder somewhere
    app-path-web = /path/to/public/files
    
    middleware-static-enable = true
    
    # optional parmeters
    middleware-static-last-modified = true
    middleware-static-max-age = 86400000
    middleware-static-etag = true
    
    # serve gzipped bundles
    middleware-static-precompressed = \.js$|\.css$
    

    One middleware for all with above config

    const { api, middleware } = require("backendjs")
    
    api.app.get(middleware.static)
    

    Routing explicitly

    Separate middleware by route

    const { api, middleware } = require("backendjs");
    const { static } = middleware;
    
    api.app.get("/blog/*", { root: "dist/", precompressed: /\.js$|/static/.+\.html$/, handle: static.handle })
    
    api.app.get("/public/*", { root: "web", noCache: true, lastModified: false, handle: static.handle })
    
Source:

Methods

(static) configureMiddleware()

Description:
  • Start global middleware, makes it the first route via #0 routing method

Source:

(static) configureMiddleware()

Description:
  • Start global middleware

Source:

(static) handle()

Description:
  • Static middleware, if root is not provided the app.home is used to prevent out of scope access, see RequestContext#sendFile for options.

Source: