Week # 03: DevOps Learning Journey - Deploying a MERN CRUD Application:
In this blog post, I’ll walk you through deploying a MERN (MongoDB, Express.js, React.js, Node.js) stack CRUD (Create, Read, Update, Delete) application. We’ll cover setting up the frontend on Netlify, deploying the backend with Railway, and using MongoDB Atlas for our database.
Prerequisites
Before we start, ensure you have the following:
A MERN stack CRUD application ready for deployment.
Accounts on Netlify, Railway, and MongoDB Atlas.
Step 1: Setting Up MongoDB Atlas
Create a MongoDB Atlas Account: If you don’t have one, sign up at MongoDB Atlas.
Create a Cluster: Follow the prompts to create a new cluster.
Database Access: Set up a database user with a strong password.
Network Access: Whitelist your IP address or set it to allow access from anywhere.
Connect to Cluster: Obtain your connection string. It should look like this:
mongodb+srv://<username>:<password>@cluster0.mongodb.net/<dbname>?retryWrites=true&w=majority
Update Environment Variables: Add this connection string to your backend environment variables (
.env
file):MONGODB_URI=mongodb+srv://<username>:<password>@cluster0.mongodb.net/<dbname>?retryWrites=true&w=majority
Step 2: Deploying the Backend on the Railway
Create a Railway Account: Sign up at Railway.
New Project: Create a new project and link it to your GitHub repository containing your backend code.
Environment Variables: Add your environment variables, including the MongoDB connection string, to Railway.
Deploy: Click deploy and wait for Railway to build and deploy your backend. Note the URL provided by Railway for your backend.
Step 3: Deploying the Frontend on Netlify
New Site: Create a new site and link it to your GitHub repository containing your frontend code.
Build Settings:
Build Command:
npm run build
(or your specific build command)Publish Directory:
build
Environment Variables: Add any necessary environment variables (e.g., API URLs).
Deploy: Click deploy and wait for Netlify to build and deploy your frontend. Netlify will provide you with a URL for your live site.
Step 4: Connecting Frontend to Backend
Update API URLs: Ensure your frontend code points to the correct backend URL provided by Railway.
Test Functionality: Go through your app to ensure all CRUD operations work seamlessly from frontend to backend.
Sample Deployed Application link
Conclusion
Deploying a MERN stack application involves setting up various services for each part of the stack. With MongoDB Atlas handling our database, Railway deploying our backend, and Netlify hosting our frontend, we have a robust, scalable, and efficient deployment setup.
Final Thoughts
This deployment process ensures that our application is live and accessible, ready to handle CRUD operations efficiently. Using these modern deployment services, we can easily scale and manage our application.
Happy coding and deploying!
Subscribe to my newsletter
Read articles from Malik Muneeb Asghar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Malik Muneeb Asghar
Malik Muneeb Asghar
Tech fanatic with a passion for problem-solving and cloud services. Expertise in React, Angular, Express, Node, and AWS. Tech visionary with a knack for problem-solving and a passion for cloud services.