Spanish Total.js
English
Documentation For AI agents
Contents

Installation and first app

Install Total.js 4, write the start script and answer your first requests.

.md GitHub
On this page

Requirements

Node.js. Total.js 4 has no dependencies and does not require a specific version of Node.js; use a current LTS release.

Install

mkdir myapp
cd myapp
npm init -y
npm install total4

The start script

Create index.js. It is the official start script of Total.js 4, reduced to what you need now:

const options = {};

// options.ip = '127.0.0.1';
// options.port = parseInt(process.argv[2]);
// options.config = { name: 'My app' };

var type = process.argv.indexOf('--release', 1) !== -1 ? 'release' : 'debug';
require('total4/' + type)(options);

Run it:

node index.js

The app listens on port 8000. In debug mode it restarts by itself when you change a file. In production start it with node index.js --release.

Your first routes

Create controllers/default.js:

exports.install = function() {
    ROUTE('GET /', home);
    ROUTE('GET /hello/{name}/', hello);
};

function home() {
    this.plain('Hello world');
}

function hello(name) {
    this.json({ hello: name });
}

Open http://127.0.0.1:8000/ and http://127.0.0.1:8000/hello/Peter/. Each action runs with the controller as this, and the dynamic parts of the URL arrive as arguments.

Options of the start script

OptionWhat it does
ip, portAddress and port of the server (default port 8000)
unixsocketListens on a Unix socket instead of a port
configValues that override the config file
sleepWaits before starting, in milliseconds
inspectorOpens the Node.js inspector on a port
watchExtra folders that restart the app in debug mode
httpskey and cert to serve HTTPS directly
cluster, cluster_limitRuns several processes ('auto' scales them)
servicemodeStarts without the web server (--servicemode)

Next

Read Routing to learn the whole syntax of ROUTE().

Made in Europe