---
title: Application structure and modes
project: Total.js V5
version: 0.0.20
section: guides
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/guides/application-structure/
updated: 2026-11-19T16:16:52.152Z
---

# Application structure and modes

> The start script, the folders and their loading order, debug and release mode, bundles, remote editing, modules and statistics.

A Total.js 5 application is a folder with a start script, a few plain files and folders with JavaScript files. The framework scans the folders in a fixed order when it starts, so a file only needs to be in the right folder to be loaded. How to install `total5` and create the first project is in the guide [Installation](../../guides/installation/).

## The start script

The start script (usually `index.js`) is the file that you run with `node`. It loads `total5` and calls `F.run()` with the start options:

```js
require('total5');

var options = {};

// options.ip = '127.0.0.1';
// options.port = parseInt(process.argv[2]);
// options.config = { name: 'Total.js' };
// options.cluster = 'auto';

F.run(options);
```

`F.run()` starts the application in **debug** mode with a watcher. With `options.watcher = false` it starts in **release** mode. Both modes are explained below; every option is in the [reference](../../reference/application-structure/).

The root of the application is the current working directory of the process (`F.directory`), so start the script from the folder of the application: `cd /www/app && node index.js`. When the script loads `total5`, the framework also changes the working directory of the process (`process.cwd()`) to the folder of the start script, except inside worker threads.

## Folders and the loading order

`LOAD()` (called by `F.http()` and `F.run()`) reads the application in this order:

1. `.env`: environment variables
2. `config`: the configuration (see [Configuration](../../guides/configuration/))
3. `version`: the first line becomes `CONF.version` as a number
4. `resources/*.resource`: dictionaries (see [Localization](../../guides/localization/))
5. `jsonschemas/*.json` and `jsonschemas/*.txt`: JSON schemas, named after the file
6. the JavaScript files of `extensions`, `modules`, `actions`, `schemas`, `models`, `definitions`, `controllers` and `middleware`, in this order
7. `plugins/`: every plugin folder: its `index.js`, then the JavaScript files of its `extensions`, `controllers`, `actions`, `schemas`, `models`, `definitions` and `middleware` folders (see [Plugins and components](../../guides/plugins-and-components/)). A folder without `index.js` is skipped. A `config` file in the plugin folder is read like the main `config`, before the JavaScript files are loaded. An `index.flow` file in the plugin folder is loaded as a FlowStream of the plugin (`PLUGINS.<id>.flowstream`)
8. `flowstreams/*.flow`: the FlowStream engine loads the flows (see [FlowStream](../../guides/flowstream/))
9. the framework statistics and the service timer

The folders are read recursively. Files with `-bk` or `_bk` in the name are skipped, which is handy for keeping a backup copy of a file next to the original. Each JavaScript file is loaded with `require()`, and when it exports an `install()` function, the framework calls it:

```js
// controllers/api.js
exports.install = function() {
	ROUTE('GET /api/time/', function($) {
		$.json({ now: new Date() });
	});
};
```

When everything is loaded, `F.isLoaded` is `true`, the number of files and the time are in `F.stats.compiled` and `F.stats.compilation`, and the events `ready` and `load` are emitted (see [Scheduling and events](../../guides/scheduling-and-events/)).

Other folders are used while the application runs, not loaded: `public` ([static files](../../guides/static-files/)), `views` ([Views](../../guides/views/)), `databases`, `tmp`, `logs`, `templates`, `private`. `F.dir()` sets their paths, and a `$dir...` configuration key moves one.

### Loading only some parts

`LOAD()` accepts a list of parts (the names are in the reference), so a background process can load only what it needs, without the HTTP server:

```js
require('total5');

LOAD('config,modules,actions', function() {
	// the application is ready, without the web server
});
```

## Debug mode and the watcher

In debug mode (`F.run()` without `watcher: false`) two processes run. The first one is the **watcher**: it starts the application in a child process and watches the files. When a file changes, it prints a line and restarts the child:

```
--- UPD --- [ 2025-03-01 10:15:22 ] --------> /controllers/api.js
```

`ADD`, `UPD` and `REM` mark added, updated and removed files, and `RES` a restart asked by the application itself with `F.restart()`.

The watcher reads these folders: `controllers`, `definitions`, `extensions`, `modules`, `models`, `schemas`, `actions`, `resources`, `source`, `workers`, `middleware`, `bundles`, `transforms`, `startup` and `plugins`, plus the folders in `options.watch`. Inside them it reacts to files with the extensions `.js`, `.ts`, `.py`, `.resource`, `.package`, `.bundle`, `.build`, `.url` and `.html`, and to files named `config` (for example `plugins/<name>/config`), but not to files inside a `public` folder. In the root folder it watches the files `config` and `version` and files ending with `.ts`, `.py` or `.resource` (other `.js` files in the root are ignored, and so is the start script). The `public` and `views` folders are not watched (except with bundles or live reload, see below). A file that is still being written (its size keeps growing between two checks, for example a slow copy over the network) is checked again in the next round, so the application restarts only when the file is complete.

The watcher polls the files every few seconds. In debug mode `DEBUG` is `true` in the application process, and the console prints the start information (`F.console()`) after the first start.

The watcher writes its PID into `<script>.pid` (`index.pid`); deleting the file stops the watcher and the application within a few seconds. When the application process ends by itself, the watcher ends too. `options.inspector` starts the child with `--inspect=<port>`.

### Live reload

With `options.livereload` set to the hostname of your application, the watcher connects to the Total.js live reload service (`wss://livereload.totaljs.com/?hostname=...`) and sends the name of every changed `.js`, `.ts`, `.py`, `.css`, `.html`, `.htm`, `.jpg`, `.png`, `.gif`, `.ico`, `.svg`, `.webp` or `.resource` file there, including changes in `public` and `views`. Other files in the watched folders are not watched in this mode.

## Release mode

```js
require('total5');
F.run({ watcher: false, port: 8000 });
```

In release mode the application runs in the same process, without the watcher, and `DEBUG` is `false`. Started with the `--watcher` argument (`node index.js --watcher`), the process becomes a watcher for release mode: it prints `RELEASE PID`, starts the application in a child process, writes `<script>.pid`, restarts the application when it ends and stops when the PID file is deleted. It does not watch the files.

### Service mode

`options.servicemode` loads the application without the HTTP server: `true` loads everything, a string such as `'config,modules,actions'` loads only these parts (the names of `LOAD()`). It works in both modes.

### Cluster

`options.cluster` starts the application in several Node.js cluster threads that share the port: a number, or `'auto'`, which starts with one thread and adds threads (up to `options.limit`) when the threads answer slowly. In debug mode the watcher restarts all threads when a file changes; the cluster runs without a watcher when `options.release` is `true`. More about threads is in [Workers and threads](../../guides/workers-and-threads/).

## Bundles

A bundle is one `.bundle` file with the files of an application or of a part of it (a module, a plugin, an admin). You create it with the `total5 bundle` command (see [Command line](../../guides/command-line/)) and place it into the `bundles` folder of another application.

When `bundles` contains files, `LOAD()` builds the application in the hidden folder `.src` and runs it from there (`F.isBundle` is `true` and `F.directory` points to `.src`):

1. Every `bundles/<name>.url` file contains a URL; the bundle is downloaded into `bundles/<name>.bundle`.
2. The files of the previous build (listed in `.src/bundle.json`) are removed.
3. Every `.bundle` file (except `-bk` and `_bk` copies) is extracted into `.src`. A file of `/updates/` is skipped when it, or its `_bk` copy, already exists in `.src`; a file of `/startup/` or `/databases/` is skipped when it already exists there.
4. The files of the application itself are copied over the extracted ones, so your own files win. The folders `tmp`, `bundles`, `logs`, `node_modules`, hidden files and folders in the root and the PID files are skipped.
5. The list of files is written to `.src/bundle.json`.

The `config` files, `sitemap`, `versions`, `workflows`, `dependencies` and `.resource` files are merged (each copy is appended), so several bundles can add keys to the same `config`. A file whose name starts with `--` (`/controllers/--api.js`) is appended to the file without the dashes.

A `.bundleignore` file in the root lists paths that are not copied, one per line: a folder (`/docs/`), a file (`/readme.md`) or the files with one extension in a folder (`/public/*.map`).

```
/docs/
/readme.md
/public/*.map
```

With the configuration key `bundling` set to `'shallow'`, the build is skipped when `.src/bundle.json` exists and the application runs from the existing `.src`. Bundles are extracted before the `config` file is read, so set the key in the start options: `F.run({ config: { bundling: 'shallow' } })`.

A file named `bundles.debug` in the root skips the build and runs the application directly from `.src`. It is for editing an extracted application in place: the debug watcher then watches `.src`.

With bundles, the debug watcher watches every file: changes in `public` and `views` are copied into `.src` without a restart, other changes rebuild `.src` and restart.

## Remote editing

The Total.js Code Editor can edit the files of a running application. The application connects to the editor with a WebSocket, so it works behind a firewall without an open port:

```js
require('total5');
F.run({ edit: 'wss://code.example.com/?id=myproject' });
```

The URL comes from the editor (`http` is changed to `ws`). The client reconnects every 10 seconds when the connection is lost, also after the answers `404 project not found` and `409 project is already open`.

The editor can browse, load, save, create, rename, remove, upload and download files, read and clear the logs and, in debug mode, restart the application. It can also run a file on the server (`exec`): a `.js` file with Node.js, a `.sh` file with `sh`, or with a command that the editor sends; the output comes back to the editor. Saved files are written into the folder of the application, so in debug mode the watcher restarts the application after each change.

In debug mode the editor client runs in the watcher process. In release mode it runs in the watcher process when the application is started with `--watcher`. `F.remote(url, [directory])` starts the same client from your own code, and `total5 edit <url>` starts it for the current folder without running the application.

### Remote editing by an AI agent

`editmcp` does the same for an AI agent. The application connects to an MCP edit proxy, and the agent works on the files through the MCP tools of the proxy:

```js
require('total5');
F.run({ editmcp: 'wss://proxy.example.com/$mcp/?id=myproject' });
```

The proxy is a Total.js application with `$mcp : true` and one WebSocket route:

```js
const editmcp = require('total5/edit-mcp');
ROUTE('SOCKET /$mcp/', $ => editmcp.proxy($));
```

The agent connects to the MCP endpoint of the proxy with the same id, `https://proxy.example.com/$mcp/?id=myproject`, and gets the tools `App_info`, `App_tree`, `App_read`, `App_write`, `App_delete`, `App_restore`, `App_move`, `App_mkdir`, `App_search`, `App_logs`, `App_shell` and `App_restart`. The proxy sends each call to the connected application, where it runs; when no application with that id is connected the answer is `App is offline.`. Paths are relative to the application root. Each `App_write` saves the written content as a numbered revision in the folder `revisions/`, and `App_restore` returns the content of an earlier revision.

`App_shell` runs any shell command on the computer of the application, so protect the endpoint of the proxy with `Total.mcp.auth` (see [MCP server](../mcp-server/)). Like `edit`, the client runs in the watcher process.

## Modules and MODS

A file in `modules` is a module: a piece of code that the rest of the application uses. The framework registers it in `MODS` (`F.modules`) under `exports.id`, or under the file name when `id` is missing, and then calls `install()`:

```js
// modules/counter.js
exports.id = 'counter';

var count = 0;

exports.install = function() {
	ROUTE('GET /counter/', function($) {
		$.json({ count: exports.increment() });
	});
};

exports.increment = function() {
	return ++count;
};

// anywhere after the start
MODS.counter.increment();
```

Plugins are registered in `PLUGINS` in the same way (see [Plugins and components](../../guides/plugins-and-components/)).

## IMPORT() and REQUIRE()

`REQUIRE(name)` loads a Node.js module or an NPM package and also looks into `CONF.$nodemodules`. `IMPORT(url)` downloads a JavaScript file into the temporary folder and loads it, once per process:

```js
var Fs = REQUIRE('fs');
var helpers = await IMPORT('https://cdn.example.com/helpers.js');
```

## Framework statistics

`F.stats` holds counters for the whole life of the process: requests by method and type, responses by type and HTTP code, transferred data, `compilation` and `compiled` from the start, and `error`.

Once a minute the service timer computes the usage statistics of the process into `F.consumption` and emits `ON('$stats', stats)`. The first statistics are computed 5 seconds after the application is loaded. When `CONF.$stats` is `true` (the default), they are also written into `<script>.json` next to the start script (`index.js.json`), so a monitoring tool can read them without asking the application:

```js
ON('$stats', function(stats) {
	if (stats.memory > 500)
		console.log('High memory usage:', stats.memory, 'MB');
});
```

The fields (memory, requests, errors, per-minute counters ...) are listed in the reference of `F.consumption`. `overload` counts the hours in which the usage was above 80 %, the memory above 600 MB or more than 1000 requests were pending. `uptime` is the number of minutes since the start.
