Deploying a MEAN Stack App to the Cloud (e.g., Heroku, AWS, Vercel)
Deploying a MEAN Stack App to the Cloud (e.g., Heroku, AWS, Vercel)
Deploying a MEAN Stack (MongoDB, Express.js, Angular, Node.js) application to the cloud involves several steps, depending on the provider. Here's a high-level guide for deploying to Heroku, AWS (EC2), and Vercel, with notes on which parts of the stack are best hosted where.
✅ PREPARE YOUR MEAN STACK APP
Structure your app (commonly):
bash
Copy
Edit
/client (Angular frontend)
/server (Node.js + Express backend)
/package.json (may be at root or in /server)
Build the Angular app:
bash
Copy
Edit
cd client
ng build --prod
This generates static files in client/dist/, which your Express app can serve.
Serve Angular from Express (Optional):
In server.js or app.js, add:
js
Copy
Edit
app.use(express.static(path.join(__dirname, 'client/dist')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client/dist/index.html'));
});
🌐 DEPLOYING TO HEROKU
Install Heroku CLI: https://devcenter.heroku.com/articles/heroku-cli
Login & create a Heroku app:
bash
Copy
Edit
heroku login
heroku create your-app-name
Add a Procfile in the root:
pgsql
Copy
Edit
web: node server/server.js
Set MongoDB using MongoDB Atlas (hosted cloud DB):
Create a cluster at https://www.mongodb.com/cloud/atlas
Add the connection string to Heroku config:
bash
Copy
Edit
heroku config:set MONGO_URI=your_mongodb_connection_string
Push to Heroku:
bash
Copy
Edit
git add .
git commit -m "Deploy"
git push heroku main
Visit your app:
bash
Copy
Edit
heroku open
☁️ DEPLOYING TO AWS EC2
Launch an EC2 instance (Ubuntu recommended).
SSH into it:
bash
Copy
Edit
ssh -i "your-key.pem" ubuntu@your-ec2-ip
Install dependencies:
bash
Copy
Edit
sudo apt update
sudo apt install nodejs npm git
Clone your app:
bash
Copy
Edit
git clone https://github.com/your/repo.git
cd repo
Install and build:
bash
Copy
Edit
cd client
npm install
ng build --prod
cd ../server
npm install
Run server:
bash
Copy
Edit
node server.js
Set up PM2 and reverse proxy:
Install PM2: npm install -g pm2
Start your app with: pm2 start server.js
Set up Nginx to forward traffic from port 80 to your app's port (e.g., 3000).
Don't forget: Open security group for HTTP and custom app ports.
⚡ DEPLOYING TO VERCEL (Frontend Only)
Vercel is frontend-focused, so it's best used to deploy only the Angular app. Use Heroku/AWS for the backend.
Install Vercel CLI:
bash
Copy
Edit
npm i -g vercel
From the client directory:
bash
Copy
Edit
cd client
vercel
It’ll prompt you through project setup.
Set backend URL in Angular:
Use environment variables (e.g., environment.prod.ts) to point to your API hosted elsewhere (Heroku, AWS).
🧠 Summary
Stack Part Heroku AWS EC2 Vercel
Angular Served via Express Nginx/Express ✅ Best Option
Node/Express ✅ Best Option ✅ Flexible 🚫 Not Supported
MongoDB Use MongoDB Atlas Use MongoDB Atlas or install on EC2 Use Atlas
Learn MEAN Stack Course
Read More
Optimizing Performance in Large MEAN Applications
Using Mongoose for Data Modeling in MongoDB MEAN
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment