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
key terms
reverse proxy
The public face: nginx takes TLS, serves static files, and forwards requests to your app privately.
nginx → localhost:3000process manager
Keeps the app alive: restarts on crash, starts on boot, and can reload without downtime.
pm2 start app.jsenv var
Config injected at runtime: port, database URL, secrets. Same code, three environments.
process.env.PORTgraceful shutdown
On SIGTERM: stop accepting new work, finish in-flight requests, close the database, then exit.
process.on("SIGTERM")how it works
- 1Get a machine
A five-dollar VPS or a platform like Render. The internet needs an address that answers.
- 2Build once, run many
Install and compile one artifact, then run it. Env vars are injected at runtime, never baked in.
- 3Give it a chaperone
pm2 or systemd starts the app, restarts it on crash, and launches it again on reboot.
- 4Put nginx in front
TLS certificates, compression, and a clean proxy to your app on localhost.
- 5Find 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
side by side
own a VPS
- full control, full chores
- cheap at small scale
- you own TLS, logs, restarts
- SSH in and poke around
use a platform
- git push and it ships
- TLS, logs, restarts handled
- costs climb as you grow
- less to learn, less to break
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