Setting Up a Basic MERN Application from Scratch πŸš€

info_generalhazedawn_a3d

Info general Hazedawn

Posted on September 19, 2024

Setting Up a Basic MERN Application from Scratch πŸš€

The MERN stack, consisting of MongoDB, Express.js, React, and Node.js, is a powerful combination for building full-stack web applications. In this blog post, we will walk you through the step-by-step process of setting up a basic MERN application from scratch, using MongoDB Atlas for cloud-based databases and deploying the app on platforms like Heroku or Vercel. Let’s dive in! 🌐

What is the MERN Stack? πŸ› οΈ

The MERN stack is a popular web development framework that allows developers to build dynamic web applications using JavaScript throughout the entire stack. Here’s a quick overview of each component:

  • MongoDB: A NoSQL database that stores data in JSON-like documents.

  • Express.js: A web application framework for Node.js that simplifies server-side development.

  • React: A JavaScript library for building user interfaces, particularly single-page applications.

  • Node.js: A JavaScript runtime that enables server-side scripting.

Step-by-Step Guide to Setting Up Your MERN Application πŸ“

Step 1: Set Up Your Development Environment 🌍

Before you start, ensure you have the following installed:

  • Node.js: Download from nodejs.org.

  • MongoDB: You can use MongoDB Atlas for a cloud-based solution. Sign up at mongodb.com/cloud/atlas.

Step 2: Create a New Node.js Project πŸ“

  1. Open your terminal and create a new directory for your project:

bash
mkdir my-mern-app
cd my-mern-app

  1. Initialize a new Node.js project:

bash
npm init -y

  1. Install required dependencies:

bash
npm install express mongoose dotenv cors

Step 3: Set Up MongoDB Atlas 🌐

  1. Create a new cluster on MongoDB Atlas and choose the free tier option.

  2. After creating your cluster, click on "Connect" and select "Connect Your Application."

  3. Copy the connection string provided and replace with your MongoDB password.

  4. Create a .env file in your project root and add your connection string:

text
MONGODB_URI=mongodb+srv://:
@cluster.mongodb.net/myFirstDatabase?
retryWrites=true&w=majority

Step 4: Create the Express Server πŸ’»

  1. Create a new file named server.js in your project root:

javascript
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
require('dotenv').config();

const app = express();
const PORT = process.env.PORT || 5000;

app.use(cors());
app.use(express.json());

mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("MongoDB connected"))
.catch(err => console.error(err));

app.listen(PORT, () => {
console.log(Server is running on port: ${PORT});
});

Step 5: Create a React Application 🎨

  1. In your project directory, create a React application:

bash
npx create-react-app client

  1. Navigate to the client directory:

bash
cd client

  1. Start the React development server:

bash
npm start

*_Step 6: Connect React Frontend with Express Backend πŸ”—_*

  1. In the client/package.json, add a proxy to your Express backend:

json
"proxy": "http://localhost:5000"

  1. This allows your React frontend to communicate with your Express backend seamlessly.

Step 7: Deploying Your MERN Application πŸš€

You can deploy your MERN application on platforms like Heroku or Vercel:

Deploying on Heroku:

  1. Install the Heroku CLI and log in:

bash
heroku login

  1. Create a new Heroku app:

bash
heroku create my-mern-app

  1. Add your MongoDB URI to Heroku config variables:

bash
heroku config:set MONGODB_URI=

  1. Push your code to Heroku:

bash
git add .
git commit -m "Initial commit"
git push heroku master

Deploying on Vercel:

  1. Install Vercel CLI and log in:

bash
npm i -g vercel
vercel login

  1. Deploy your React app:

bash
vercel --prod

Conclusion πŸŽ‰

Congratulations! You have successfully set up a basic MERN application from scratch using MongoDB Atlas and deployed it on platforms like Heroku or Vercel. The MERN stack provides a robust framework for building full-stack applications efficiently using JavaScript.

By mastering the MERN stack, you can create dynamic web applications that are scalable and maintainable. Keep exploring and building more complex features as you grow in your development journey! πŸ’»βœ¨

MERNStack #WebDevelopment #JavaScript #FullStackDevelopment #MongoDB #ReactJS #NodeJS #ExpressJS #CodingJourney

πŸ’– πŸ’ͺ πŸ™… 🚩
info_generalhazedawn_a3d
Info general Hazedawn

Posted on September 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related