Express Sweet API Reference - v3.0.0
    Preparing search index...

    Interface BasicConfig

    Express Sweet basic application configuration interface. Defines configuration options for environment, CORS, routing, error handling, and more.

    // config/config.js
    export default {
    env_path: '.env',
    cors_enabled: true,
    max_body_size: '100kb',
    router_dir: 'routes',
    default_router: '/blog',
    rewrite_base_url: baseUrl => `${baseUrl}/admin`,
    is_ajax: req => /^/api/.test(req.path),
    hook_handle_error: (error, req, res, next) => {
    if (error.status === 404) res.render('error/404');
    else res.render('error/500');
    }
    };
    interface BasicConfig {
        env_path?: string;
        cors_enabled?: boolean;
        max_body_size?: string | number;
        router_dir?: string;
        default_router?: string;
        rewrite_base_url?: (baseUrl: string) => string;
        is_ajax: (req: Request) => boolean;
        hook_handle_error?: (
            error: any,
            req: Request,
            res: Response,
            next: NextFunction,
        ) => void;
    }
    Index

    Properties

    env_path?: string

    Environment variable file (.env) path, defaults to none (undefined).

    cors_enabled?: boolean

    CORS permission, defaults to invalid (false).

    max_body_size?: string | number

    Maximum body size you can request, defaults to 100kb.

    router_dir?: string

    Absolute path to the router directory, defaults to <application root directory>/routes.

    default_router?: string

    The endpoint to run when the root URL is requested, defaults to none (undefined).

    rewrite_base_url?: (baseUrl: string) => string

    This is a hook that rewrites the base URL. If you want to rewrite the app.locals.baseUrl property and the view's baseUrl variable, use this hook to return a new base URL. The default value is the referrer's origin (eg https://example.com).

    rewrite_base_url: baseUrl => {
    return `${baseUrl}/admin`;
    }
    is_ajax: (req: Request) => boolean

    How to determine if it is an ajax request. The default is that if there is an XMLHttpRequest in the request header (req.xhr) returns true. For example, if there is no XMLHttpRequest in req(express.Request) and the Ajax endpoint starts with /api, a custom Ajax decision can be made like "return /^/api//.test(req.path)".

    is_ajax: req => {
    // If the request URL begins with /api, it is assumed to be Ajax.
    return /^/api/.test(req.path);
    // return !!req.xhr;
    }
    hook_handle_error?: (
        error: any,
        req: Request,
        res: Response,
        next: NextFunction,
    ) => void

    Hooks the default behavior on request errors. If unset, simply returns an error HTTP status. (res.status(error.status||500).end();)

    hook_handle_error: (error, req, res, next) => {
    if (error.status === 404)
    // If the URL cannot be found, a 404 error screen (views/errors/404.hbs) is displayed.
    res.render('errors/404');
    else
    // For other errors, unknown error screen (views/error/500.hbs) is displayed.
    res.render('error/500');
    },