Master

Deploying your app

Deploying means putting your app on a server everyone can reach. Learn environment variables, process managers, and the steps to take your app from your laptop to the internet.

What you will see

Running on 3 identical instances behind a load balancer Scale up by adding more identical machines: True

About 2 min of reading

Try It

Deploy with twelve-factor principles

1

Keep config in the environment.

def config(): return {"database_url": "sqlite:///app.db", "api_key": "from-env"} print() print("Deploying your app") print("Config from environment: %s" % json.dumps(config()))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Deploying your app Config from environment: {"database_url": "sqlite:///app.db", "api_key": "from-env"}
Config lives in the environment, never the code.
2

Stream logs to stdout.

print("Request received") print("Query executed") print("Response sent") print("Logs streamed to stdout: True")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Request received Query executed Response sent Logs streamed to stdout: True
3

Run identical instances behind a load balancer.

instances = ["node-1", "node-2", "node-3"] print("Running on %d identical instances behind a load balancer" % len(instances)) print("Scale up by adding more identical machines: True")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Running on 3 identical instances behind a load balancer Scale up by adding more identical machines: True

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

The server starts under a process manager, reads config from the environment, and stays up across restarts while logs stream out.
1 PM2 starts server.js
↓
2 The process reads environment variables
↓
3 The app boots and opens its port
↓
4 The process manager monitors health
↓
5 A crash triggers an automatic restart
↓
6 Logs are streamed to the platform
Why this works
The twelve-factor mindset

Modern apps follow the twelve-factor principles, the most important being:

  • Config lives in the environment, never the code.
  • State is stored outside the process (a database), so any instance can handle any request.
  • Logs are streamed to stdout, never written to files.

An app built this way can run on many servers at once, behind a load balancer, and scale up by simply adding more identical machines. That is how apps serve millions of users.

Examples

Configuration with env vars

Never hard-code secrets. Read them from environment variables:

const port = process.env.PORT || 3000;
const secret = process.env.SESSION_SECRET;

Then set them on the server, never in your code. A .env file is handy locally but should be git-ignored so it never reaches version control:

SESSION_SECRET=super-secret-value
PORT=8080

The same app code runs unchanged in development, staging, and production, only the environment differs.

Keeping the process alive

If your Node process crashes, the app goes down. A process manager restarts it automatically. PM2 is the common one:

npm install -g pm2
pm2 start server.js --name myapp
pm2 save

PM2 keeps your app running, restarts it after crashes or reboots, and logs all output. For production you deploy to a platform that manages all of this for you, a cloud server, or a platform like Render or Railway.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Automated testing
Previous Lesson
Automated testing