Deploying an API

localhost:3000 is not a production URL. Sorry.

▶ the lesson · narrated sketch video · tap for sound

the intro

Deploying means putting your API on a machine the internet can reach: a VPS with nginx and a process manager, or a platform that handles it. The skills that matter: environment config, logs, restarts, and zero-downtime updates.

fun fact ✦

“Works on my machine” ends the day you deploy — that is why environments must match.

the docs

the deep dive

the ideas behind the video — concepts, diagrams, a walkthrough and the traps to avoid

localhost dies the moment you close the laptop. Production is the same code wearing four new pieces of ceremony: a process manager that restarts it, a proxy that fronts it, configuration that arrives from the environment, and logs that tell you the truth. You wire those up once, and they babysit forever.

same code, new chaperones

usernginx :443app :3000the DB
prod traffic enters through the proxy, never straight into your app

key terms

reverse proxy

The public face: nginx takes TLS, serves static files, and forwards requests to your app privately.

nginx → localhost:3000

process manager

Keeps the app alive: restarts on crash, starts on boot, and can reload without downtime.

pm2 start app.js

env var

Config injected at runtime: port, database URL, secrets. Same code, three environments.

process.env.PORT

graceful shutdown

On SIGTERM: stop accepting new work, finish in-flight requests, close the database, then exit.

process.on("SIGTERM")

how it works

  1. 1
    Get a machine

    A five-dollar VPS or a platform like Render. The internet needs an address that answers.

  2. 2
    Build once, run many

    Install and compile one artifact, then run it. Env vars are injected at runtime, never baked in.

  3. 3
    Give it a chaperone

    pm2 or systemd starts the app, restarts it on crash, and launches it again on reboot.

  4. 4
    Put nginx in front

    TLS certificates, compression, and a clean proxy to your app on localhost.

  5. 5
    Find the logs before you need them

    journalctl -u app for the service, /var/log/nginx for the proxy. When prod breaks, go here first.

the code, explained

the nginx piece: public HTTPS in, private HTTP out

1server {
2 listen 443 ssl;
TLS ends here — the padlock is the proxy's job
3 location / {
4 proxy_pass http://localhost:3000;
a private hand-off to your app on localhost
5 }
6}

side by side

own a VPS

  • full control, full chores
  • cheap at small scale
  • you own TLS, logs, restarts
  • SSH in and poke around
VS

use a platform

  • git push and it ships
  • TLS, logs, restarts handled
  • costs climb as you grow
  • less to learn, less to break
one $5 VPSnginxapp + pm2logsTLS certs
production is often just one small box wearing four hats
🪤 gotcha

Works locally, dead in prod? Check what it binds to first. An app listening on 127.0.0.1 inside the server box is unreachable from outside; it must listen on 0.0.0.0, or let the platform set PORT. It is the number one deploy surprise, and the fix is one line.

quick answers

Do I need Docker to deploy?

No. pm2 on a small VPS is a perfectly real production setup. Docker earns its keep when you need identical environments across many machines and teammates.

How do I ship an update without dropping users?

Run two or more instances, reload them one at a time, and let old workers finish in-flight requests. pm2 cluster mode does this in one command.

Where should secrets live in production?

In the platform's settings or an env file that never touches git. If a secret is in your repo, treat it as burned and rotate it.

quick hits about Deploying an API

short answers · interviews, tips, trivia & hidden gems

🎤 interview

What is a reverse proxy?watch ↗

🎤 interview

Dev, staging, prod?watch ↗

📚 tutorial

What happens when you deploy?watch ↗

🔧 useful

Where do errors hide on a VPS?watch ↗

🔧 useful

Config in production?watch ↗

💎 deep

Deploy without dropping users?watch ↗

💎 deep

What is graceful shutdown?watch ↗