Spanish Total.js
English
Documentation For AI agents
Contents

Configuration

The config file and its types, .env, CONF, LOADCONFIG() and DEBUG.

.md GitHub
On this page

The configuration of a Total.js 5 application lives in a plain text file named config in the root directory of the application. The framework reads it when it starts and copies every key into the global object CONF (the same object as F.config). Your code and the framework read the values from there.

name             : My application
$api             : /api/
$httpmaxsize     : 1024
$cors            : https://app.example.com
mail_from        : info@example.com
smtp (json)      : {"server":"smtp.example.com","port":465,"user":"info@example.com","password":"secret"}
// anywhere in the code
console.log(CONF.name);       // My application
console.log(F.config.$api);   // /api/

Keys that start with $ are settings of the framework. Other keys (name, mail_from, your own keys) are free to use. Most $ keys have a default value, so the file only needs the keys you change. The reference of Configuration lists them all.

The format of the file

Each line is key : value. The framework splits the line at the first colon and skips one character after it, so always put a space after the colon. Spaces before the colon and around the value are removed, which lets you align the values in columns. Values may contain more colons (URLs, times).

  • Empty lines and lines that start with # or // are ignored.
  • true and false become booleans, and a value of one digit (0 to 9) becomes a number. Every other value without a type is a string.
  • A value that starts with base64 or hex is decoded to a string first.

A type in parentheses after the key converts the value:

TypeResult
(number), (float), (double), (currency)A number
(boolean), (bool)true when the value contains true, on, 1 or enabled
(array)An array of strings, split by commas and trimmed
(json), (object)The parsed JSON
(eval)The result of a JavaScript expression
(date), (time), (datetime)A Date
(env), (environment)The value of the environment variable with that name
(config)The value of another key that is already in CONF
(random)A new random identifier of the given length (default 10) on every start
(generate), (hash)A random string of the given length (default 10), created once and kept
$httptimeout (number)  : 10
allowed (array)        : admin, editor
limits (json)          : {"files":10,"size":5}
started (date)         : 2025-01-01
db (env)               : DATABASE_URL
appname (config)       : name
token (generate)       : 32
logo                   : base64 aGVsbG8=

(generate) and (hash) values are stored in databases/config.json the first time and read from there on every later start, so a generated secret does not change after a restart. Delete the key from that file to create a new one.

(config) reads CONF before the file is applied: it sees the defaults and the values loaded earlier, not the keys of the same file.

Use (number) for every numeric value of your own keys.

Environment variables and .env

Before the config file, the framework reads the file .env in the root directory, if it exists. Each line is KEY=value, with no spaces around =. Lines that start with # or // are ignored, quotes around the value are removed and \n becomes a new line. A key is copied into process.env only when the variable is not already set, so the real environment always wins.

# .env
DATABASE_URL=postgresql://user:pass@localhost:5432/app
API_KEY="abc123"

The config file reads them with the (env) type:

database (env)   : DATABASE_URL
apikey (env)     : API_KEY

The framework itself reads a few variables: PORT (see the port below), and it sets TZ from $timezone and NODE_TLS_REJECT_UNAUTHORIZED from $insecure.

Loading order

When the application starts, the framework loads, in this order:

  1. The config object from the options of F.http() (for example F.http({ config: { name: 'Shop' } })).
  2. The file .env.
  3. The file config. Its values win over the options.
  4. The file version: its first line, as a number, becomes CONF.version.
  5. The config file of each plugin, before the other files of the plugins.

Only after that does it load modules, actions, controllers and the other files, so the configuration is ready when your code runs. F.load() with a list of types (for example LOAD('config,modules')) loads only those parts; env and config are types too.

In debug mode the watcher restarts the application when the config file of the application or of a plugin changes.

Plugin configuration

A plugin can bring its own config file: plugins/<name>/config, next to the index.js of the plugin. It has the same format and is loaded with LOADCONFIG() after the config of the application, so its keys are added to the same global CONF and overwrite keys with the same name. Prefix the keys with the name of the plugin to avoid conflicts.

# plugins/invoices/config
invoices_currency   : EUR
invoices_due (number) : 14
// plugins/invoices/index.js
exports.install = function() {
    var days = CONF.invoices_due;
};

See Plugins and components for the rest of the plugin structure.

Common keys

KeyDefaultMeaning
nameTotal.jsThe name of the application: page titles, e-mails, the start screen
version1.0.0The version; also read from the version file
author''Printed on the start screen
secretThe key for ENCRYPT()/DECRYPT() and signed values; set your own
secret_csrf''Enables CSRF tokens; see Authorization
secret_encryption''The key for encrypted WebSocket messages
mail_from, mail_from_name, mail_reply, mail_cc, mail_bccDefaults for MAIL(); see Mail
smtp (or mail)The SMTP options as JSON; mail_smtp and mail_smtp_options set the server and the options separately
$root''A prefix for all routes and static files, e.g. /app/
$api/api/The URL that replaces ? in API routes
$port, $ipauto, 0.0.0.0Where the HTTP server listens
$timezoneutcThe time zone of the process (TZ)
$httpmaxsize256The maximum size of a request body in kB
$httptimeout5The request timeout in seconds
$cors''Allowed origins for cross-origin requests

$port stays auto when you leave it out: the server then uses the port option of F.http(), the PORT variable, a number in the command line arguments, or 8000, in this order.

Changing the configuration at runtime

LOADCONFIG(value) applies new values while the application runs. It accepts the text of a config file, an object, or an array of { id, type, value } items. In an object or an array, the values false, 0 and '' are stored as undefined; use the text form for them. Keys with a special meaning ($root, $api, $timezone, $httpfiles, $cryptoiv, smtp…) are processed the same way as in the file, and the framework then emits the $reconfigure event.

// from a database, for example
LOADCONFIG({ name: 'Shop', $cors: 'https://shop.example.com' });

LOADCONFIG('$httptimeout (number) : 15\n$minifyhtml : false');

ON('$reconfigure', function() {
    console.log('New name:', CONF.name);
});

You can also assign CONF.key = value directly. That changes only the value: the derived settings (the CORS list, the time zone, TMS, the logger) are refreshed only by LOADCONFIG().

DEBUG

The global DEBUG is true in debug mode and false in release mode. The framework uses it for caching, minification and the source map, and you can use it too:

if (DEBUG)
    console.log('Running in debug mode');

DEBUG starts as process.env.NODE_ENV !== 'production': it is false when NODE_ENV is production and true otherwise. Total.run() sets it before the application loads: false with options.release = true (the official start script sets it with --release) or with options.watcher = false, and true otherwise. So the variable decides the mode only when the application calls F.http() or F.load() without Total.run(). It is not a config key.

Made in Europe