Deploying MEAN Stack Applications on Heroku
The MEAN stack—MongoDB, Express.js, Angular, and Node.js—is a popular full-stack JavaScript framework for building dynamic web applications. Once you’ve built your MEAN stack app locally, the next step is to deploy it so users can access it online. Heroku, a cloud platform that supports Node.js, offers an easy and efficient way to host full-stack apps. In this blog, we’ll walk through the process of deploying a MEAN stack application on Heroku.
Step 1: Prepare Your Project for Deployment
Before deployment, ensure your project is production-ready:
Combine Frontend & Backend: Use ng build --prod to compile your Angular app. Copy the output folder (dist/) into the backend's public directory.
Update server code: Serve static files using Express:
app.use(express.static(path.join(__dirname, 'dist/<angular-folder-name>')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/<angular-folder-name>/index.html'));
});
Environment Variables: Replace local database URIs or keys with environment variable references for security.
Step 2: Set Up MongoDB Atlas (Optional)
If your app uses MongoDB, use MongoDB Atlas (a cloud-based MongoDB service):
Create a MongoDB Atlas account.
Create a cluster and whitelist your IP.
Get the connection URI and store it in an environment variable like MONGO_URI.
Step 3: Initialize a Git Repository
If you haven't already:
git init
git add .
git commit -m "Initial commit"
Step 4: Create a Heroku App
Install the Heroku CLI if not already installed. Then:
heroku login
heroku create your-app-name
Set necessary environment variables:
heroku config:set MONGO_URI=your_mongo_connection_string
Step 5: Deploy to Heroku
Push your code to Heroku:
git push heroku master
Heroku will install dependencies, build the app, and start the server using the start script in package.json.
Step 6: Open Your Live App
After successful deployment, run:
heroku open
Your MEAN stack application is now live!
Conclusion
Deploying a MEAN stack app on Heroku is a smooth process when done step-by-step. By integrating MongoDB Atlas and using Heroku’s seamless Git-based workflow, developers can launch scalable web applications with minimal infrastructure overhead. Perfect for startups, prototypes, or personal projects!
Learn MEAN Stack Training Course
Read more:
Top Companies Hiring MEAN Stack Developers
Learning MEAN Stack as a Complete Beginner
Creating RESTful APIs with MEAN Stack
How Angular Integrates with Node.js in MEAN Stack
Visit Quality Thought Training Institute
Get Direction
Comments
Post a Comment