Overview
The Adoptme API is deployed on Railway, a modern platform-as-a-service that simplifies deployment and hosting. This guide covers deploying the application to Railway with proper configuration.Live Deployment
Production URL: https://coderhouse-backendiii-production.up.railway.appSwagger Documentation: https://coderhouse-backendiii-production.up.railway.app/docs
Prerequisites
- Railway account (railway.app)
- GitHub repository with your code
- MongoDB database (Railway MongoDB addon or external service like MongoDB Atlas)
- Railway CLI (optional, for command-line deployment)
Deployment Steps
Create New Project
- Log in to Railway
- Click “New Project”
- Select “Deploy from GitHub repo”
- Authorize Railway to access your GitHub account
- Select the repository containing the Adoptme API
Configure Build Settings
Railway automatically detects Node.js projects and configures:
- Build Command:
npm install - Start Command:
npm start - Node Version: Detected from Dockerfile (20.11.0)
Set Environment Variables
In the Railway dashboard, navigate to your project and add the following environment variables:
Configure MongoDB
Option 1: Railway MongoDB Plugin
- In your Railway project, click “New”
- Select “Database” → “MongoDB”
- Railway will provision a MongoDB instance
- Use the provided
MONGO_URLconnection string
- Create a MongoDB Atlas cluster
- Get your connection string
- Add it to Railway as
MONGO_URL - Ensure your Atlas cluster allows connections from Railway IPs (0.0.0.0/0 for simplicity, or specific Railway IPs)
Deploy
Railway automatically deploys when you push to your connected GitHub branch:Monitor the deployment in the Railway dashboard under the “Deployments” tab.
Environment Configuration
Required Environment Variables
| Variable | Description | Example | Required |
|---|---|---|---|
PORT | Application port | 3000 | Yes |
MONGO_URL | MongoDB connection URL | mongodb+srv://user:[email protected] | Yes |
DB_NAME | Database name | adoptme | Yes |
Setting Variables in Railway
Environment variables set in Railway are automatically injected into your application at runtime. No need to commit
.env files.MongoDB Connection in Production
Connection String Format
Railway requires a full MongoDB connection string:Security Considerations
- Use strong passwords for MongoDB users
- Restrict MongoDB network access to Railway IPs when possible
- Enable MongoDB authentication
- Use connection string parameters like
retryWrites=truefor reliability
Health Checks and Monitoring
Health Check Endpoint
Railway automatically monitors your application. Configure a health check endpoint in your Railway project settings:- Path:
/api/health(if available) - Port:
3000 - Interval: 30 seconds
Monitoring Deployment
- Logs: View real-time logs in Railway dashboard under “Logs” tab
- Metrics: Monitor CPU, memory, and network usage in “Metrics” tab
- Deployments: Track deployment history and rollback if needed
Testing the Production Deployment
Continuous Deployment
Railway automatically deploys on every push to your configured branch:-
Push to GitHub:
- Railway detects changes: Automatically triggers a new deployment
-
Build process: Runs
npm installand builds your application - Deploy: Replaces the old deployment with the new one (zero-downtime)
- Health check: Ensures the new deployment is healthy before routing traffic
Troubleshooting
Deployment Fails
- Check build logs in Railway dashboard
- Verify all environment variables are set correctly
- Ensure
package.jsonhas correctstartscript - Check for syntax errors or missing dependencies
Application Crashes on Startup
- Verify
MONGO_URLandDB_NAMEare set - Check MongoDB is accessible from Railway
- Review application logs for connection errors
- Ensure Node.js version compatibility
Cannot Connect to MongoDB
- Verify MongoDB allows connections from Railway (check IP whitelist)
- Test connection string format (include authentication database)
- Check MongoDB user permissions
- Ensure database exists or app can create it
502 Bad Gateway
- Application may still be starting (wait 30-60 seconds)
- Check if application is listening on correct PORT
- Verify application binds to
0.0.0.0notlocalhost - Review logs for startup errors
For more advanced Railway features, explore their documentation at docs.railway.app
Rollback and Recovery
If a deployment fails or causes issues:- Go to “Deployments” tab in Railway dashboard
- Find the last working deployment
- Click “Redeploy” to rollback
- Fix the issue in your code
- Push a new commit to deploy again