ForgeTotal.js 5
en

Total.js 5 (backend)

Routes, actions, schemas, databases, WebSocket, views and everything that runs on the server.

RecipeTags

Validate a JSON body with an action

Define an input schema on an action and let Total.js reject invalid requests before your code runs.

routingactionsvalidation

Expose actions through one API endpoint

Declare ROUTE('API ...') endpoints so a client can call many actions with POST {schema, data} on a single URL.

routingapiactions

Read URL parameters in a route

Declare dynamic segments such as {id} in a route and read them from $.params.

routingparams

Catch many URLs with a wildcard route

End a route with * to match everything below a path, and keep specific routes for the exceptions.

routing

Handle each HTTP method

Declare routes for GET, POST, PUT, PATCH and DELETE and read the method from $.method.

routinghttp

Restrict a route with flags

Add @flags such as @json, @xml or @upload so a route only matches the requests it is meant for.

routingflags

Read the query string and the request body

Use $.query for the URL parameters and $.body for JSON or form data in a route handler.

routingrequest

Limit the payload size and the timeout of a route

Append <1kb or <10s to a route to cap the request size or the time the handler may take.

routingsecuritylimits

Declare typed route parameters in an action

Use the params option of NEWACTION so Total.js validates the URL parameters before your code runs.

actionsroutingvalidation

Choose how to respond

Send JSON, HTML, plain text, XML, redirects, empty responses or binary data from a route handler.

routingresponse

Read headers and work with cookies

Read request headers from $.headers, read and write cookies with $.cookie() and set response headers.

routingheaderscookies

Receive file uploads

Accept multipart/form-data with the @upload flag and read the uploaded files from $.files.

routinguploadfiles

Allow cross-origin requests with CORS

Call CORS() to allow one origin, several origins or every origin to use your routes from a browser.

securitycorsrouting

Render a view without writing a handler

A route with no handler renders the view named after its URL, or the view you name.

routingviews

Pick the right input type for an action

Choose types for input, params, query and output definitions, including conversions and rejected values.

actionsvalidationtypes

Validate the query string in an action

Declare query in NEWACTION to validate and convert the URL parameters, and read the clean values from $.query.

actionsvalidationquery

Filter the response of an action with output

Declare output so only the listed fields leave the action, for example to never leak a password hash.

actionssecurity

Require a user or a permission in an action

Use the user and permissions options of NEWACTION so authorization lives next to the action, not in every route.

actionsauthsecurity

Protect routes with AUTH

Authenticate each request with AUTH() and restrict routes to signed-in or anonymous users with the + and - prefixes.

authroutingsecurity

Declare the route inside the action

Put the route in the route option of NEWACTION and keep the URL, the validation and the code in one place.

actionsrouting

Sign users in with the built-in session auth

Pass an options object to AUTH() to get signed session cookies, a session store and logout without writing the plumbing.

authsessionssecurity

Call an action from your own code

Use ACTION() to run an action from a timer, a job or another action, with its own payload, query and user.

actions

Chain several actions in one route

List several actions after --> to run them in sequence over the same payload and mark which one answers with (response).

actionsrouting

Add a middleware to a route

Register a middleware with MIDDLEWARE() and attach it to routes with the #name flag.

middlewareroutingsecurity

Build a WebSocket chat

Declare a SOCKET route, listen to messages and broadcast them to every connected client.

websocketrealtime

Connect to a WebSocket server from Node

Use WEBSOCKETCLIENT to open a WebSocket connection from your app, send messages and react to the answers.

websocketrealtimeintegrations

Decouple your code with events

Emit and listen to application events with EMIT(), ON(), ONCE() and OFF() so modules do not call each other directly.

eventsarchitecture

Store and read data with the embedded NoSQL database

Insert and query documents with NOSQL() and expose them through actions, without installing any database.

nosqldatabaseactions

Query the NoSQL database

Filter, sort, page and project documents with where, in, between, search, sort, take, skip and fields.

nosqldatabasequeries

Update and remove NoSQL documents

Change documents with update() and where(), delete them with remove(), and read how many were affected.

nosqldatabase

Use the DATA API to switch databases

DATA addresses a table as database/table, so the same query code works with the embedded NoSQL and with other databases.

databasenosqlquerybuilder

Cache values in memory

Store values with CACHE.set() and an expiration, read them with CACHE.get() and clear them with remove() or reset().

cacheperformance

Persist small settings with MEMORIZE

Keep a JSON object on disk that survives restarts, for counters and settings that do not need a database.

storagesettings

Generate unique ids and hashes

Create ids with UID() and GUID(), and hashes with HASH(), and know which one to use where.

utilitiesidssecurity

Cache slow results on disk with FILECACHE

Use FILECACHE() to compute a result once and reuse it from a file for a period, even after a restart.

cacheperformancefiles

Store uploaded files with FILESTORAGE

Save uploads in a managed file storage with file.fs() and send them back with $.filefs().

filesuploadstorage

Render a view with a layout

Render an HTML view with a model, loop over data and pass values to the layout.

viewslayouttemplates

Use conditions and escaping in views

Branch with @{if}, @{else} and @{fi}, and choose between escaped output @{value} and raw HTML @{!value}.

viewstemplates

Loop over arrays and objects in views

Repeat markup with @{foreach} for arrays and objects, and use the index of the current item.

viewstemplates

Include views and assets

Reuse markup with @{view()} and add scripts and styles with @{import()}.

viewstemplatesassets

Fill sections of the layout from a view

Declare a section in a view with @{section} and print it in the layout with @{section('name')}, and pick another layout per request.

viewslayouttemplates

Add view helpers and read the config in views

Register functions in DEF.helpers to call them from views, and read config values with @{'%key'}.

viewstemplatesconfig

Convert Markdown to HTML

Use the String.markdown() prototype to render Markdown text into HTML, for content stored by users or in files.

contentmarkdown

Render text with Tangular templates

Use Tangular.render() to fill templates for e-mails, documents and messages with conditions, loops and helpers.

templatescontent

Render templates from text, files and URLs with TEMPLATE

Use TEMPLATE() to render a Tangular template that is a text, a file of the templates folder or a URL, with its own helpers and a default model.

templatescontenttangular

Compare two lists with DIFFARR: what to add, update and remove

Use DIFFARR() to compare the items saved in the database with the items of a form and get the ones to add, to update and to remove.

utilitiesarraysforms

Query builder: DB() and NEWDB() with your own database driver

Build queries with DB().find().where().sort() and let a driver registered with NEWDB() run them, while a registered driver implements supported database operations.

databasequerybuilder

Configure the app with the config file

Keep settings in a config file and read them through CONF.

configsettings

Encrypt and decrypt data with a key

Turn objects into opaque tokens with ENCRYPT() and read them back with DECRYPT(), for signed links and short-lived tokens.

securitycrypto

Call external services with API() and NEWAPI() drivers

Register a driver for a service with NEWAPI() and call it from actions with API(name, schema, data), with the same callback, error and promise style as the query builder.

apiservicesintegrations

Talk to language models with AIMODEL(), NEWAIMODEL() and AIPARSER()

Build a chat request with AIMODEL(), let a driver registered with NEWAIMODEL() send it to a provider, and read streamed answers with AIPARSER().

aillmintegrations

Limit failed attempts with BLOCKED

Count attempts per IP address with BLOCKED() and answer 429 once the limit is reached, for logins and password resets.

securityrate-limiting

The small global helpers: MAIN, REPO, FUNC, NOW, CLONE, COPY, QUERIFY and PROMISIFY

Use application globals, dates, deep copies, query strings and promise helpers.

globalsutilitieshelpers

Collect errors with ErrorBuilder, log them with ERROR() and keep an audit trail with AUDIT()

Answer with several errors at once using ErrorBuilder, the SUCCESS() answer, how ERROR() records failures, and how AUDIT() appends who did what to a log file.

errorsloggingaudit

Debounce work with setTimeout2() and clearTimeout2()

Use setTimeout2() to run a function once after the calls stop coming, replacing the previous timer with the same id, and cancel it with clearTimeout2().

timersdebounceutilities

Tokens tied to a browser with ENCRYPTREQ() and DECRYPTREQ()

Encrypt a value together with the user agent (and optionally the IP address) of the request, and read it back only from the same client.

securityencryptiontokens

Back up a folder with BACKUP() and RESTORE(), and run commands with SHELL()

Pack a directory into one file and unpack it again with BACKUP() and RESTORE(), and run a shell command in the project folder with SHELL().

backupshellfiles

Put the application in maintenance with PAUSESERVER()

Pause the server by name so that every request gets a 503 page, resume it, and know that without a second argument the call toggles.

maintenanceserveravailability

Business rules as text with NEWMACRO()

Compile small rules written in a simple language (IF, ELSE, AND, OR, RETURN, helper calls) into a function that you run with a model, so the rules can live in a file or a database.

macrosrulesscripting

Serve static files from the public folder

Files in the public directory are served automatically, no route required.

staticfiles

Call another address with AJAX() from the server

Use AJAX() to call an HTTP address with a JSON body and get the parsed answer, as a promise or with a callback, and know what happens with errors and XML.

httprequestsajax

Call other HTTP APIs with RESTBuilder

Make GET and POST requests to another service, set headers and a timeout, and read the status and the parsed response.

integrationshttprestbuilder

Release a stream when it ends with CLEANUP()

Use CLEANUP() to destroy a stream as soon as it has finished or failed, so that file handles are not left open, with a callback or as a promise.

streamsfilescleanup

Translate a view into several languages

Mark texts with @(...) in views and provide translations in resource files selected by LOCALIZE().

localizationi18nviews

Run code on a schedule with CRON

Register a function with CRON() and a five-field cron line, and know when the check runs and how to remove a job.

cronschedulerjobs

Run heavy work in a thread or a child process

Use NEWTHREAD() to start a worker thread, send it messages and read the replies, without blocking the main process.

workerthreadperformance

Share a few worker threads with NEWTHREADPOOL()

Start a fixed number of workers, borrow one with pool.exec() when there is work, give it back with release(), and let the other jobs wait for a free worker.

workerthreadpoolperformance

Call other servers with REQUEST and save files with DOWNLOAD

Use REQUEST() for low-level HTTP calls with full control over method, headers, query and body, and DOWNLOAD() to stream a remote file to disk.

httprequestdownload

Send an email from a view

Configure SMTP in the config file, render a view as the message body with MAIL(), and send plain HTML with HTMLMAIL().

mailsmtpemail

Customize the 404 and 401 pages

Register ROUTE('#404') and ROUTE('#401') handlers to replace the built-in error pages, and trigger them yourself with $.fallback().

errors404routing

Mail yourself a message with LOGMAIL()

Send a plain text or JSON message to an address without a view, for logs and alerts, through the same SMTP settings as MAIL().

mailsmtplogging

Send files and streams to the client

Serve a file from disk with $.file(), force a download name, stream generated content with $.stream() and answer with 404 when the file is missing.

filesdownloadstream

Forward requests to another server

Use PROXY() to map a URL prefix to an upstream server, choose how the path is copied, and $.proxy() to forward a single route.

proxygatewayintegrations

Publish a local server through a tunnel with PROXYSERVER() and PROXYCLIENT()

Let a Total.js server with a public address forward requests to a program that only runs on a private machine, with a WebSocket tunnel that the private side opens.

proxytunnelwebsocketintegrations

Protect forms and requests with CSRF tokens

Create a token with $.csrf(), send it with the request and verify it in a middleware so cross-site requests are rejected.

securitycsrfmiddleware

Group actions in a schema and reuse input definitions

Use NEWSCHEMA() to declare related actions under one name, and share an input definition between actions with a file in jsonschemas/.

schemaactionsvalidation

Build a processing pipeline with transforms

Register several steps under one name with NEWTRANSFORM() and run a value through them in order with TRANSFORM().

transformpipeline

Exchange messages with the Total Messaging Service

Enable the TMS WebSocket endpoint and let external clients receive published messages, send messages to your app and call its functions.

tmspublishsubscribewebsocketmessaging

Build a complete REST resource with actions

Declare query, read, insert, update, patch and remove actions over the NoSQL database, with validation, a 404 for missing records and routes that map each HTTP method.

actionscrudrestnosql

Configure a WebSocket route with flags, auth and parameters

Choose the message type with @text or @binary, require a signed-in user with the + prefix, check a CSRF token with @csrf and read URL parameters in SOCKET routes.

websocketrealtimesecurity

Minify and merge CSS, JavaScript and HTML

Static files and HTML responses are minified automatically; use MERGE() to serve several files as one and the $minify settings to switch it off.

performancecssjavascripthtmlminification

Resize and convert images

Process images with Image.load(), chain resize, crop and format operations, save the result, measure its size and serve thumbnails with $.image().

imagesthumbnailsgraphicsmagick

Verify numeric schema boundaries instead of assuming integer coercion

Check Int, SmallInt, TinyInt and Number2 with native compact schemas, including fractional values, zero and out-of-range inputs.

validationschemanumbersboundaries

Use the built-in string, number, date and array helpers

Total.js extends String, Number, Date and Array with helpers for slugs, hashes, formatting, parsing, grouping and searching, and offers U.extend() and U.clone() for objects.

utilitieshelpersstringarraydate

Record action audit events with a native audit sink

Observe the audit option and explicit $.audit() calls without contacting a logging service or writing sensitive input to an application log.

actionsauditsecurity

Run in debug and release mode

The options of Total.run() choose between debug and release mode; read the mode from DEBUG and limit routes to one mode with @debug and @release.

deploymentdebugreleaseenvironment

Test the application with the built-in test runner

Write a test.js that starts the app, registers tests with Test.push(), checks results with Test.print() and exits with a non-zero code when one fails.

testingci

Use all CPU cores with the cluster mode

Start several worker processes behind one port with the cluster option of Total.run(), and know what is shared between them and what is not.

clusterperformancedeployment

Load a JavaScript module from a URL with IMPORT()

Download a file with JavaScript from an address, keep it in the tmp folder, require it and use what it exports; the second call does not download it again.

modulesdownloaddependencies

Build a flow of components with Flow.load() and run it with exec()

Describe a FlowStream as components and the connections between them, load it in the application, send a message through it and read the answer, and change the configuration of a component while it runs.

flowflowstreamcomponentsautomation

Test a Flow component alone with Flow.test()

Load one component in a flow of its own, send it a message from the code and read what it sends to its outputs, without building the whole flow.

flowflowstreamcomponentstests

Call a Python program that keeps running with PYPELINE()

Start a Python script from the application, send it JSON messages over a socket and receive its answers, with the script restarted if it dies.

pythonpypelineintegrationsai

Enums and string-format validation boundaries

Run positive and negative compact-schema checks for enums, Color, Base64, Zip, Icon, JSON and IP formats.

total5reference

TotalAPI and Componentator service boundaries

Verify the disabled TotalAPI branch and Componentator local-cache reuse without making external service calls.

total5reference

QueryBuilder operations, filters and driver contract

Inspect native DB and NEWDB options, queued results, upsert, errors and filter shapes without confusing a builder with a database driver.

total5reference

Compile and run a native server component

Compile trusted Total.js component HTML, create an instance, validate messages, reconfigure it and release its resources.

total5reference

Controller callbacks, manual authorization and conditional responses

Execute jsonstring, respond, done, successful, authorize, custom and Last-Modified handling.

total5reference

Action transport options and explicit TMS publishing

Test csrf, encrypt, compress and publish against real HTTP and TMS rather than assuming metadata enforces behavior.

total5reference

Invalidate release-mode static caches with TOUCH

Serve and modify a temporary static file, clear its cached representation and remove cached 404 entries.

total5reference

OPENCLIENT RPC, events and shared connections

Exercise OPENCLIENT with a local protocol fixture, including initialization, callback correlation, timeout and logical-client removal.

total5reference

Load FlowStream files and execute isolated workers

Run the same trusted FlowStream schema as a worker thread and a child process, load .flow files and remove instances.

total5reference

Action lifecycle hooks, result extension and administrator guards

Exercise oninput, onoutput, extend, sa and su with valid, invalid and failed actions.

total5reference

Real SQL with native QueryBuilder and SQLite transactions

Register a bounded SQLite driver, run parameterized CRUD and verify commit, constraint failure and rollback against a file-backed database.

total5reference

Load application resources and avoid runtime package surprises

Run LOAD in a separate process and verify NPMINSTALL reuse of an already available package without network access.

total5reference

Query a real local LDAP directory

Query an isolated OpenLDAP instance and verify the bind result in total5 0.0.19.

total5reference

Package application files as a Total.js bundle

Create a local bundle, load its definitions through the framework startup loader and verify extraction into .src.

total5reference

Own uploaded-file cleanup and build query links

Verify default upload cleanup, autoclear(false), explicit clear and href query replacement.

total5reference