# The running app

> What every app gets and has to do while it runs: the port and address to listen on, the variables set for you, no root rights, a temporary disk, the stop signal.

Whatever the framework, an app on the platform is a Node.js process in a replica. This page says what that process gets and what it has to do. The [guide of your framework](https://arrangic.com/docs/frameworks.md) says how to do it in its idiom.

## Port and address

The platform sets the `PORT` variable and sends every request to that port. It is 3000 unless you change it under **Settings**, **Build & deploy**.

- **Read the port from `PORT`.** A port written into the code only works by accident.
- **1024 or higher.** Lower ports are not open to an app, and the settings refuse them.
- **Listen on all interfaces.** Give no host, or `0.0.0.0`. A server on `localhost` or `127.0.0.1` cannot be reached, and the deployment fails with "nothing listens on port N, so make sure the app listens on 0.0.0.0:N, not only on 127.0.0.1".
- **One port.** Only this port gets traffic. A second server on another port is not reachable.
- **Plain HTTP.** HTTPS is handled before a request reaches your app. Do not load certificates in the app. See [Requests and sockets](https://arrangic.com/docs/requests.md).

A React, Vue or Angular app, and any other app the platform serves as built files, has nothing to do here: the platform's own server listens for it.

## Starting in time

The new version has 2 minutes to start listening, or, with a [health check path](https://arrangic.com/docs/deployments.md#health-check), the wait you set there to answer on that path. If it takes longer, the deployment fails and the old version stays.

Do slow one-off work before the app starts, not inside it. Database migrations belong in [deploy commands](https://arrangic.com/docs/deploy-commands.md).

## Variables set for you

| Variable | Value |
| --- | --- |
| `PORT` | The port from the [build settings](https://arrangic.com/docs/build-settings.md#port) |
| `NODE_ENV` | `production`, for the build and for the running app |

You cannot change or remove them. `APP_URL` is not set for you: if your code reads it, add it under **Settings**, **Variables** with the address you want, and change it yourself when the address changes. Names that start with `PLATFORM_` are kept for the platform as well. Some frameworks get a few more, named in their guide (AdonisJS and Astro). Everything else comes from the [environment variables](https://arrangic.com/docs/environment-variables.md) you add.

## No root rights

The app runs as a user without root rights, and so do the [deploy commands](https://arrangic.com/docs/deploy-commands.md) and [cron jobs](https://arrangic.com/docs/cron-jobs.md). The install and the build run with full rights.

- **Files.** The app can write in its own folder, in its home folder and in `/tmp`. Writing anywhere else fails with "permission denied" or `EACCES`.
- **Ports.** A port below 1024 cannot be used.
- **System packages.** Nothing can be installed while the app runs. Only the npm packages in your `package.json` are there.

## A temporary disk

The disk of a replica is temporary. A file written while the app runs is gone after the next deployment or restart, and it can be gone after the app has slept. Replicas do not share files.

- **Database.** Use a hosted database and put its address in a variable, `DATABASE_URL` for example. SQLite in a file does not survive.
- **Uploads.** Send them to a file store outside the app, not to a folder.
- **Logs.** Print to the console. A log written to a file is on the disk of one replica, where you cannot read it. See [Logs and crashes](https://arrangic.com/docs/logs.md).

## Memory per replica

Memory is temporary in the same way, and it is not shared between replicas. Sessions, caches, counters and rate limits kept in memory are gone at every deployment and differ between replicas. Keep them in a database or a store outside the app. `express-session` without a store is the usual case.

An app that uses more memory than its [size](https://arrangic.com/docs/scaling.md#instance-size) has is stopped and started again; the runtime log says so.

## Stopping

To stop a replica, the platform sends `SIGTERM` and waits 30 seconds. Close the server on that signal, so open requests can finish:

```js
process.on('SIGTERM', () => {
	server.close(() => process.exit(0));
});
```

A replica stops at every deployment, at a restart, at a change of scaling and when the app sleeps.

## Crashes

A replica that exits with an error is started again, up to 3 times. After that it is left stopped, the overview says so and the team gets a notification. The last lines before the exit are in the runtime log. See [Logs and crashes](https://arrangic.com/docs/logs.md).

## Timers and jobs

An idle app sleeps and wakes only for a request. A timer, a cron package or a queue worker inside the app does not run while it sleeps, and with more than one replica it runs on every one of them. Keep **Keep one replica always on** switched on for such an app, or move the work out of the app: a [cron job](https://arrangic.com/docs/cron-jobs.md) for work on a schedule, a [worker](https://arrangic.com/docs/workers.md) for a queue worker or any command that keeps running. See [Sleep and wake](https://arrangic.com/docs/sleep-and-wake.md).

---

This page on the site: https://arrangic.com/docs/running-app  
All pages of the documentation: https://arrangic.com/llms.txt
