Stop 13 · Testing & DeploymentTopics · 6/10

Deploying the Express API

Part of the MERN Stack Roadmap

In short

An API on localhost helps nobody else. Deploying puts it on a public URL that your React app and your interviewer can use, and it teaches you about environment variables, ports and logs in the real world.

Your Express API runs on a Node hosting platform such as Render or Railway, or on a VPS with PM2. You set environment variables, a start command and a health check.

The jaadu carries boxes of tiffins and a recipe book into a bright new food court stall, while a manager hands over a set of keys.

Think of it like this

Moving from home kitchen to a food stall

Your tiffin business ran from home, where you knew where everything was. Now you rent a stall in a food court. You bring the recipes (code), get the gas connection and licence numbers from the management (environment variables), use the counter number they assign (PORT), and they check every morning that your stall is open (health check).

Watch it happen

From git push to a live API

Push
Install
Env vars
npm start
Health check

1/5You push to main on GitHub. Render or Railway notices the new commit.

The big ideas

What you need to know

Listen on process.env.PORT

Hosting platforms choose the port. Hardcoding 5000 is the most common reason a deploy never comes up.

Secrets in the dashboard

Set env vars on the platform. Never commit .env, and use different secrets from development.

A health route

GET /api/health returns 200 when the app and database are fine. Platforms and uptime monitors use it.

VPS option

On a VPS, run the app with PM2 so it restarts on crash and on reboot, and put Nginx in front for HTTPS.

See it in code

Walk through the code

js
1// server/src/server.js2import mongoose from "mongoose";3import app from "./app.js";4 5const PORT = process.env.PORT || 5000;6 7app.set("trust proxy", 1);8 9app.get("/api/health", (req, res) => {10  const dbUp = mongoose.connection.readyState === 1;11  res.status(dbUp ? 200 : 503).json({ ok: dbUp });12});13 14try {15  await mongoose.connect(process.env.MONGO_URI);16  app.listen(PORT, () => console.log("API listening on port " + PORT));17} catch (err) {18  console.error("Could not connect to MongoDB", err);19  process.exit(1);20}21 22// package.json23// "type": "module",24// "engines": { "node": ">=20" },25// "scripts": { "start": "node src/server.js", "dev": "node --watch src/server.js" }

1/5Use the platform's port. The fallback is only for your laptop.

A production-ready entry point: platform port, health check, fail fast if the database is down.

Save yourself the pain

Common mistakes

Oops

Using nodemon as the production start command.

Do this instead

nodemon is for development. Production should run node src/server.js, and the platform or PM2 handles restarts.

Oops

Leaving cors({ origin: "*" }) in production.

Do this instead

Set origin to your real frontend URL from an env var like CLIENT_URL.

Oops

Debugging a failed deploy by guessing.

Do this instead

Read the platform's build and runtime logs first. They nearly always name the missing env var or crash.

Your mission

Put your API online

~60 min

Quick check

Your deploy logs say "API listening on port 5000" but the platform says the service never became healthy. What is the likely bug?

Go deeper

  1. 1Deploy the API to a free tier of Render or Railway.
  2. 2Set NODE_ENV, MONGO_URI, JWT secrets and the allowed CORS origin in the dashboard.
  3. 3Add a /api/health route the platform can check.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge