ForgeDocumentation
en

Documentation

From Express to Total.js 5

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

Total.js is not Express with another name. The biggest differences: a handler receives one object, $, instead of (req, res); the body is parsed for you by its content type; you do not install packages for cookies, uploads, validation, sessions, CORS, cron or email, because they are part of the framework; and the files of your project are loaded from folders instead of being imported (see the project structure page).

The table pairs common Express patterns with their Total.js 5 equivalents.

JobExpressTotal.js 5
Start the serverapp.listen(3000)Total.run({ port: 3000 })
A routeapp.get('/users', fn)ROUTE('GET /users/', fn) (write the end slash)
Path parameterapp.get('/users/:id', fn) and req.params.idROUTE('GET /users/{id}/', fn) and $.params.id
Query stringreq.query.page$.query.page
JSON bodyapp.use(express.json()) and req.body$.body, parsed by its content type
Request headersreq.headers['x-token']$.headers['x-token']
Send JSONres.json(object)$.json(object)
Send text or HTMLres.send('text')$.plain('text') or $.html('<p>..</p>')
Status coderes.status(404).json(...)$.invalid(404) for an error, or $.response.status = 201 before answering
Response headerres.set('x-a', 'b')$.response.headers['x-a'] = 'b'
Redirectres.redirect('/x')$.redirect('/x')
Cookiescookie-parser, res.cookie(...)$.cookie('name') to read, $.cookie('name', value, '1 day') to write
Middleware on a routeapp.get('/a', mw, fn)MIDDLEWARE('mw', ($, next) => ...) and ROUTE('GET /a/ #mw', fn)
Static filesapp.use(express.static('public'))Put the files in public/: no code
Download a fileres.download(path, name)$.file(path, name)
Templateres.render('page', model)$.view('page', model)
Validate inputexpress-validator or zodinput: '*email:Email, age:Number' on an action
Error pagean error-handling middlewareROUTE('#404', fn) and ROUTE('#401', fn)
Authenticationpassport or express-sessionAUTH(fn) and ROUTE('+GET /private/', fn)
Uploadsmulterthe flag @upload and $.files
CORScorsCORS('https://example.com')
Call another APIaxios or fetchREQUEST() and RESTBuilder
Scheduled jobsnode-cronCRON('0 9 * * 1-5', fn)
Send emailnodemailerMAIL() and HTMLMAIL() with mail_smtp in config
WebSocketws or socket.ioROUTE('SOCKET /chat/', fn)

Each part has a recipe with more detail; this is the whole mapping in one place. The example below is the Total.js side of the rows that need code.

definitions/compare.js
MIDDLEWARE('mw', function($, next) {
	$.response.headers['x-a'] = 'b';
	next();
});

NEWACTION('Users/create', {
	name: 'Create a user',
	input: '*email:Email, age:Number',
	action: function($, model) {
		$.callback({ email: model.email, age: model.age });
	}
});
controllers/compare.js
exports.install = function() {
	ROUTE('GET /users/{id}/', user);
	ROUTE('POST /users/ --> *Users/create');
	ROUTE('GET /mw/ #mw', ok);
	ROUTE('GET /created/', created);
	ROUTE('GET /moved/', moved);
	ROUTE('GET /cookie/', cookie);
	ROUTE('GET /html/', html);
};

function user($) {
	$.json({ id: $.params.id, page: $.query.page, token: $.headers['x-token'] });
}

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

function created($) {
	$.response.status = 201;
	$.json({ created: true });
}

function moved($) {
	$.redirect('/users/1/');
}

function cookie($) {
	$.cookie('visited', 'yes', '1 day');
	$.json({ before: $.cookie('visited') || null });
}

function html($) {
	$.html('<p>Hello</p>');
}