From Express to 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.
| Job | Express | Total.js 5 |
|---|---|---|
| Start the server | app.listen(3000) | Total.run({ port: 3000 }) |
| A route | app.get('/users', fn) | ROUTE('GET /users/', fn) (write the end slash) |
| Path parameter | app.get('/users/:id', fn) and req.params.id | ROUTE('GET /users/{id}/', fn) and $.params.id |
| Query string | req.query.page | $.query.page |
| JSON body | app.use(express.json()) and req.body | $.body, parsed by its content type |
| Request headers | req.headers['x-token'] | $.headers['x-token'] |
| Send JSON | res.json(object) | $.json(object) |
| Send text or HTML | res.send('text') | $.plain('text') or $.html('<p>..</p>') |
| Status code | res.status(404).json(...) | $.invalid(404) for an error, or $.response.status = 201 before answering |
| Response header | res.set('x-a', 'b') | $.response.headers['x-a'] = 'b' |
| Redirect | res.redirect('/x') | $.redirect('/x') |
| Cookies | cookie-parser, res.cookie(...) | $.cookie('name') to read, $.cookie('name', value, '1 day') to write |
| Middleware on a route | app.get('/a', mw, fn) | MIDDLEWARE('mw', ($, next) => ...) and ROUTE('GET /a/ #mw', fn) |
| Static files | app.use(express.static('public')) | Put the files in public/: no code |
| Download a file | res.download(path, name) | $.file(path, name) |
| Template | res.render('page', model) | $.view('page', model) |
| Validate input | express-validator or zod | input: '*email:Email, age:Number' on an action |
| Error page | an error-handling middleware | ROUTE('#404', fn) and ROUTE('#401', fn) |
| Authentication | passport or express-session | AUTH(fn) and ROUTE('+GET /private/', fn) |
| Uploads | multer | the flag @upload and $.files |
| CORS | cors | CORS('https://example.com') |
| Call another API | axios or fetch | REQUEST() and RESTBuilder |
| Scheduled jobs | node-cron | CRON('0 9 * * 1-5', fn) |
| Send email | nodemailer | MAIL() and HTMLMAIL() with mail_smtp in config |
| WebSocket | ws or socket.io | ROUTE('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.
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 });
}
});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>');
}