Running in production
Run a Total.js application on a server: no watcher, the right address, automatic restarts, nginx, clean stops and logs.
On this page
This guide takes an application from node index.js on your computer to a server: it starts without the watcher, listens on the right address, restarts when the process ends, runs behind nginx, and stops and logs in a predictable way. It uses the start script from Installation.
Start without the watcher
In the start script, uncomment this line:
options.watcher = false;Then start the application from its directory:
$ node index.jsThe application now runs in that single process. No watcher starts and no index.pid file is written. DEBUG is false and the start information prints Mode : release. A change to a source file has no effect until the next start.
The working directory is the application root, so a start from another directory does not find the routes of the application.
The --release argument alone is not enough for production. It sets DEBUG to false, but the watcher keeps running in a separate process and restarts the application when a file changes.
Choose the address and the port
By default the application listens on all network interfaces. Set the address and the port in the start script:
options.ip = '127.0.0.1';
options.port = 8000;Or set them in the config file:
$ip : 127.0.0.1
$port : 8000The port comes from the first of these that is set:
| Order | Source | Example |
|---|---|---|
| 1 | The start option | options.port = 8000; |
| 2 | The configuration key | $port : 8000 |
| 3 | A number in the command line | node index.js 8001 |
| 4 | The default | 8000 |
The address follows the same rule: options.ip wins over $ip. When the port is already in use, the application prints a message and exits with code 1.
With the address 127.0.0.1, only programs on the same machine can connect. Use it when a reverse proxy runs on the same machine and connects to http://127.0.0.1:8000. A proxy in another container cannot reach that address and answers 502. In that case keep the default address and do not publish the port of the application, as in the setup below.
Keep the application running
An uncaught exception or an unhandled promise rejection does not stop the application. The framework logs the error and keeps serving requests. To react to these errors, create definitions/errors.js:
ON('error', function(obj) {
console.log('Application error:', obj.error);
});When the process does end, nothing starts it again, so something outside the application has to do it. With Docker, that is the restart policy of the container. Create a Dockerfile in the application root, next to package.json and package-lock.json:
FROM node:24-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .
RUN chown -R node:node /app
USER node
CMD ["node", "index.js"]Create .dockerignore:
node_modules
tmp
logsCreate compose.yaml. The nginx service and its nginx.conf file are described in the next section:
services:
app:
build: .
restart: unless-stopped
nginx:
image: nginx:1.29-alpine
restart: unless-stopped
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
- appBuild and start both services:
$ docker compose up -d --buildWith restart: unless-stopped, Docker starts the container again each time the process exits, whether the exit code is 0 or 1. An application that you stop with docker compose stop stays stopped. To see how many times the container has been restarted:
$ docker inspect -f '{{.RestartCount}}' $(docker compose ps -a -q app)During a start the port opens shortly before the routes are loaded, so requests in that moment get a 404 response. A health check should therefore expect 200 from a route of your application.
Put nginx in front
Create nginx.conf next to compose.yaml:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
location / {
proxy_pass http://app:8000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}proxy_pass uses the service name app and the default port 8000. Only nginx publishes a port, so clients cannot connect to the application directly.
The map block and the Upgrade and Connection headers pass WebSocket connections through the proxy, on the same address as the HTTP requests. No other setting is needed for idle connections: the framework pings its WebSocket clients, and this keeps a connection without messages open through nginx.
While the application is down, nginx answers 502. When the application is back, nginx reaches it again without a restart of nginx.
What the application sees behind the proxy
The proxy_set_header lines decide what the application reads from a request:
| Value | Source | Result |
|---|---|---|
$.ip | First address in X-Forwarded-For | The address of the client |
client.ip (WebSocket) | First address in X-Forwarded-For | The address of the client |
$.protocol | X-Forwarded-Proto | https when the header is https, otherwise http |
$.host | Host | The host name the client asked for |
$.hostname('/x') | X-Forwarded-Proto and Host | https://www.example.com/x |
$.address | X-Forwarded-Proto, Host and the URL | https://www.example.com/info/ |
The framework trusts X-Forwarded-For on every request and reads its first address. Two rules follow from this:
- Send
X-Forwarded-For $remote_addr, as in the configuration above, so nginx replaces whatever the client sent. With$proxy_add_x_forwarded_for, nginx appends to the value from the client, and a client can choose the address that$.ipreturns. - Do not let clients connect to the application directly, because they can then send the header themselves.
Stop the application cleanly
The signals SIGTERM and SIGINT stop the application. docker stop and docker compose stop send SIGTERM, and the container ends in less than three seconds.
After the signal:
- The
exitevent runs. - The application refuses new connections.
- About 300 ms later the process exits with code
1.
The exit code is 1 after a clean stop too, so do not read it as a crash.
The application does not wait for running requests. A request that ends within those 300 ms gets its response. A longer request gets no response, and WebSocket clients are disconnected.
Use the exit event for short work, for example in definitions/exit.js:
const Fs = require('node:fs');
ON('exit', function() {
Fs.appendFileSync(PATH.root('stopped.log'), new Date().toISOString() + '\n');
});Keep this work synchronous or shorter than 300 ms, because the process ends after that time. The signal SIGKILL ends the process at once and the exit event does not run.
Logs
The application writes no log file by default. All output goes to the standard streams of the process:
| Output | Stream |
|---|---|
console.log() | stdout |
| The start information | stdout |
Errors logged by the framework (ERROR ======= ...) | stdout |
console.warn() and console.error() | stderr |
Docker collects both streams:
$ docker compose logs appTo write to a file instead, add this key to the config file:
$logger : trueconsole.log(), console.warn(), console.error() and the errors logged by the framework then go to logs/debug.log, and new lines are added to the file after each restart. The start information stays on stdout. To hide it, set $imprint : false.