Get started with Total.js 5
Total.js 5 is the npm package total5. You need Node.js (the examples of this site run on Node.js 24) and an empty folder. Create the project and install the package:
mkdir hello && cd hello
npm init -y
npm install total5The start script is a file named index.js in the root of the project. It loads the framework and starts the server:
require('total5');
Total.run({ port: 8000 });That is all the code you write to start. The files in the folders of the project (see the project structure page) are loaded automatically. Create these three files.
A controller declares the routes. ROUTE('GET /') with no handler would show the view with the same name; here the handler renders views/index.html with a model:
exports.install = function() {
ROUTE('GET /', home);
ROUTE('POST /greet/ --> *Greet/say');
};
function home($) {
$.view('index', { title: 'Hello Total.js' });
}An action has its own validation. The route above runs Greet/say with the JSON body of the request, and the framework rejects a body that does not match input:
NEWACTION('Greet/say', {
name: 'Say hello',
input: '*name:String',
action: function($, model) {
$.callback({ message: 'Hello, ' + model.name + '!' });
}
});A view is HTML with @{...} blocks. views/layout.html wraps every view and @{body} is the place of the view:
<!DOCTYPE html>
<html>
<head><meta charset="utf-8" /><title>Hello</title></head>
<body>@{body}</body>
</html><h1>@{model.title}</h1>Start the application with node index.js. In debug mode (the default) the application restarts when you change a file and the address is printed in the terminal. To run it like on a server use node index.js --release, and read the release mode page for what changes.