DEV Community

Cover image for How to Host a Mongoose-Powered App on Fly.io
Arunangshu Das
Arunangshu Das

Posted on

How to Host a Mongoose-Powered App on Fly.io

If you're building a full-stack JavaScript application using Node.js, Express, and MongoDB with Mongoose, you're in good company. These technologies offer developers flexibility, performance, and a rich ecosystem of tools. But when it's time to deploy your Mongoose-powered app to the world, the question arises: Where and how should you host it?

One increasingly popular answer is Fly.io—a global application hosting platform that makes it surprisingly simple to deploy backend services close to your users. Fly.io gives you speed, scalability, and even stateful service support (hello, databases!) without the DevOps headache.

In this blog post, you'll learn exactly how to host a Mongoose-powered Node.js app on Fly.io. We'll walk through everything—from setting up your local app to deploying it globally—with practical code snippets, command-line magic, and tips for success.

Why Choose Fly.io for Your Mongoose App?

Before we jump into deployment, let’s talk briefly about why Fly.io is a great fit for hosting your Mongoose app:

  • Global Deployments: Run your app near your users in multiple regions.
  • Docker Without the Headache: Fly can build your Docker image automatically.
  • Free Tier: Generous free limits for hobby projects.
  • Database-Ready: You can host MongoDB elsewhere and connect seamlessly via private or public networking.

The key takeaway? Fly.io brings the best of edge computing with a smooth developer experience, making it ideal for startups, solo devs, and hobbyists alike.

Prerequisites

Before starting, make sure you have:

  • A working Mongoose app (Node.js + Express + Mongoose).
  • MongoDB URI (can be hosted on MongoDB Atlas or elsewhere).
  • Fly.io CLI installed (flyctl).
  • Docker installed (Fly can use Docker to build your image).
  • A Fly.io account (sign up at https://fly.io).

Step 1: Prepare Your Mongoose App for Deployment

If you already have a Mongoose-powered app, great! If not, here's a simple example to work with:

Sample index.js (Express + Mongoose)

const express = require('express');
const mongoose = require('mongoose');
require('dotenv').config();
 
const app = express();
const PORT = process.env.PORT || 3000;
 
mongoose.connect(process.env.MONGODB_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true
})
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('MongoDB connection error:', err));
 
app.get('/', (req, res) => {
  res.send('Hello from Fly.io with Mongoose!');
});
 
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});
Enter fullscreen mode Exit fullscreen mode

Add a .env File

PORT=8080
MONGODB_URI=mongodb+srv://<username>:<password>@cluster.mongodb.net/mydb
Enter fullscreen mode Exit fullscreen mode

Note: Never commit your .env file. Use environment secrets on Fly.io instead.

Step 2: Add a Dockerfile for Fly.io Deployment

Fly.io uses Docker to package and deploy your app. Here’s a simple Dockerfile:

# Use official Node.js image
FROM node:18
 
# Create app directory
WORKDIR /app
 
# Copy package files
COPY package*.json ./
 
# Install dependencies
RUN npm install
 
# Copy the rest of the app
COPY . .
 
# Expose the port
EXPOSE 8080
 
# Start the app
CMD ["node", "index.js"]
Enter fullscreen mode Exit fullscreen mode

This image installs dependencies, copies your app files, and runs your server on port 8080.

Step 3: Initialize Fly.io and Deploy

Now we’re ready to push the app live.

1. Login to Fly.io

flyctl auth login
Enter fullscreen mode Exit fullscreen mode

2. Initialize a Fly.io App

flyctl launch
Enter fullscreen mode Exit fullscreen mode

You’ll be prompted to:

  • Name your app
  • Choose a region
  • Create a fly.toml file

Accept default options unless you want to customize.

Tip: Name your app something unique like my-mongoose-app.

3. Add Environment Secrets for MongoDB

Instead of shipping your .env, use Fly.io secrets:

flyctl secrets set MONGODB_URI="your-mongodb-connection-string"
Enter fullscreen mode Exit fullscreen mode

You can set any environment variable this way. These are securely stored and injected at runtime.

4. Deploy Your App

Once everything is set, deploy it:

flyctl deploy
Enter fullscreen mode Exit fullscreen mode

This command builds your Docker image, pushes it to Fly’s registry, and starts a new VM.

You’ll see output like:

==> Monitoring deployment
Logs: https://fly.io/apps/my-mongoose-app
App is live at https://my-mongoose-app.fly.dev
Enter fullscreen mode Exit fullscreen mode

Click the URL, and boom! Your Mongoose-powered app is live.

Step 4: Scaling and Region Control

Fly.io isn’t just for deploying—it’s also about running your app closer to your users.

You can scale your app to other regions with:

flyctl regions add sin
Enter fullscreen mode Exit fullscreen mode

Want more instances?

flyctl scale count 3
Enter fullscreen mode Exit fullscreen mode

Need more CPU or memory?

flyctl scale vm shared-cpu-2x
Enter fullscreen mode Exit fullscreen mode

Scaling MongoDB itself on Fly.io is more complex. For now, it’s best to use MongoDB Atlas (which has a generous free tier) and connect securely via Fly.io networking.

Step 5: Monitoring, Logs, and Troubleshooting

You can tail logs with:

 

flyctl logs
Enter fullscreen mode Exit fullscreen mode

Useful when your app isn’t starting properly.

Want to ssh into the VM?

flyctl ssh console
Enter fullscreen mode Exit fullscreen mode

Need to restart?

flyctl restart
Enter fullscreen mode Exit fullscreen mode

Debugging MongoDB errors? Make sure your MongoDB URI is correct, your IP is whitelisted (for Atlas), and you're not accidentally using localhost (which refers to the Fly VM).

Bonus: Custom Domain and HTTPS

Fly.io gives you HTTPS out of the box for their .fly.dev domains. But if you want a custom domain:

1. Add the domain:

flyctl certs add yourdomain.com
Enter fullscreen mode Exit fullscreen mode

2. Update your DNS

Fly will show you what DNS records to add. Once that’s done, HTTPS is auto-configured.

 

Bonus: Using PM2 for Better Process Management (Optional)

If you want more robust process handling (especially for larger apps), you can use PM2:

Install it:

npm install pm2 --save
Enter fullscreen mode Exit fullscreen mode

Update Dockerfile:

# Add this to CMD
CMD ["npx", "pm2-runtime", "index.js"]
Enter fullscreen mode Exit fullscreen mode

This gives you features like auto-restarts, clustering, and logging.

Real-World Tips

Here are some practical takeaways from deploying several Mongoose apps on Fly:

  1. Don’t host MongoDB on Fly unless you really know what you're doing.
       Use Atlas or another managed provider.

  2. Secure your secrets.
       Never hard-code credentials or rely on .env in production.

  3. Test locally with Docker.
       Run docker build . and docker run -p 8080:8080 your-image before deploying.

  4. Use health checks.
       You can add this to your fly.toml:

   toml
   [checks]
     [checks.http]
       path = "/"
       interval = "10s"
       timeout = "5s"
  

  1. Stay lean.    Keep your Docker image size small. Use .dockerignore, remove unused dependencies.

Final Folder Structure

my-mongoose-app/
│
├── index.js
├── package.json
├── .env                # local only
├── Dockerfile
├── fly.toml
└── README.md
Enter fullscreen mode Exit fullscreen mode

You may also like:

  1. 10 Common Mistakes with Synchronous Code in Node.js

  2. Why 85% of Developers Use Express.js Wrongly

  3. Implementing Zero-Downtime Deployments in Node.js

  4. 10 Common Memory Management Mistakes in Node.js

  5. 5 Key Differences Between ^ and ~ in package.json

  6. Scaling Node.js for Robust Multi-Tenant Architectures

  7. 6 Common Mistakes in Domain-Driven Design (DDD) with Express.js

  8. 10 Performance Enhancements in Node.js Using V8

  9. Can Node.js Handle Millions of Users?

  10. Express.js Secrets That Senior Developers Don’t Share

Read more blogs from Here

Share your experiences in the comments, and let’s discuss how to tackle them!

Follow me on Linkedin

Top comments (0)