ForgeTotal.js 5
en

Total.js 5

Run in debug and release mode

Total.js 5 · total5@0.0.19 deploymentdebugreleaseenvironment Executed against Total.js 5

Total.js runs in one of two modes, debug for development and release for the server, and the global boolean DEBUG tells your code which one is active. In debug mode the app runs under a file watcher that restarts it when you change a file, errors show details and source maps are generated. Release mode has no watcher and error pages without the technical details.

The mode is chosen by how the app is started, in the start script (index.js) that calls Total.run(options):

  • the default is debug, with the watcher;
  • options.release = true starts release mode; the start script that Total.js generates sets it from the command line, so node index.js --release is the way to start production;
  • options.watcher = false runs a single process without the watcher, and that is always release mode (it is what the examples of this cookbook use).

Before Total.run() has decided, DEBUG has a default taken from NODE_ENV: it is false when NODE_ENV=production and true otherwise. Code that runs at the moment a module is loaded sees that default, so do not rely on NODE_ENV for the final mode, only on DEBUG after the app has started.

The routes can follow the mode. The flag @debug makes a route exist only in debug mode and @release only in release mode; in the other mode the URL answers 404. Use @debug for tools you need while developing (a database viewer, a seed route) so they can never reach production by accident.

controllers/mode.js
exports.install = function() {
	ROUTE('GET /mode/', mode);
	ROUTE('GET /tools/ @debug', tools);
	ROUTE('GET /status/ @release', status);
};

function mode($) {
	$.json({ debug: DEBUG });
}

function tools($) {
	$.json({ tools: true });
}

function status($) {
	$.json({ status: 'ok' });
}