Installation and first app
Install Total.js 4, write the start script and answer your first requests.
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 total4The 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.jsThe 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
| Option | What it does |
|---|---|
ip, port | Address and port of the server (default port 8000) |
unixsocket | Listens on a Unix socket instead of a port |
config | Values that override the config file |
sleep | Waits before starting, in milliseconds |
inspector | Opens the Node.js inspector on a port |
watch | Extra folders that restart the app in debug mode |
https | key and cert to serve HTTPS directly |
cluster, cluster_limit | Runs several processes ('auto' scales them) |
servicemode | Starts without the web server (--servicemode) |
Next
Read Routing to learn the whole syntax of ROUTE().