Deploying to Render.com

In this lesson, we'll deploy our Express API to Render.com. Before diving into the deployment process, there are several important preparation steps to ensure a smooth deployment.

<aside> ⚠️

Render will automatically deploy your application whenever you push to your main branch. This includes direct pushes and merged pull requests.

</aside>

Pre-Deployment Checklist

Before deploying, we need to prepare our application and codebase. Follow these steps carefully:

1. Create Production Database

Your local development database won't be accessible in production. You'll need to set up a production database:

<aside> 💡

Just create a new DB using Neon like before and copy its DB URL. These databases expire after 72 hours if you don’t make a neon account and claim them!

</aside>

Once you have your production database, add it to your local .env file as the DATABASE_URL. Just comment out your local one for now. We will now generate our first migration and run a migration on the DB (which will do nothing because we just did a push with the same changes).

# Generate migration files
npm run db:generate
# Apply migrations to the DB
npm run db:migrate

<aside> 🔴

Don’t run the db push command on your production or staging DB’s! That is just for your dev DB’s. Always generate a migration from your schema changes and use whatever process you have have setup to apply that migration in production.

</aside>

2. Configure Dynamic Port

Hosting providers assign ports dynamically. Update your server to use environment variables:

// src/index.ts or server.ts
const PORT = process.env.PORT || 3000;

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

<aside> 🔴

Never hardcode the port number in production. Always use process.env.PORT

</aside>

3. Prepare Environment Variables

Create a list of all environment variables your app needs in production:

# Production environment variables
DATABASE_URL="your-production-database-url"
JWT_SECRET="your-secure-jwt-secret"
NODE_ENV="production"
# Add any other variables your app needs

<aside> 🔐

Security Tip: Generate new, secure values for production. Never reuse development secrets.

</aside>

4. Replace Hardcoded URLs