Welcome to our comprehensive tutorial on web development using the MERN stack!
What is MERN stack? MERN stack is a combination of four powerful technologies – MongoDB, Express.js, React, and Node.js. MongoDB is a NoSQL database, Express.js is a backend framework for Node.js, React is a front-end library for building interactive user interfaces, and Node.js is a JavaScript runtime environment for executing server-side code.
In this tutorial, we will cover the steps to build a simple web application using the MERN stack. By the end of this tutorial, you will have a basic understanding of how these technologies work together to create a dynamic and interactive web application.
Prerequisites:
- Knowledge of HTML, CSS, and JavaScript
- Node.js installed on your system
- Text editor (e.g., VS Code, Sublime Text)
Let’s get started!
Step 1: Setting Up the Project
- Create a new directory for your project and navigate to it in your terminal.
- Run the following command to create a new React app using create-react-app:
npx create-react-app my-mern-app
Step 2: Setting Up the Backend
- Inside your project directory, create a new folder called ‘backend’.
- Navigate to the ‘backend’ folder in your terminal and run the following command to initialize a new Node.js project:
npm init -y - Install Express.js and mongoose using the following command:
npm install express mongoose
Step 3: Creating the Server
- Create a new file called ‘server.js’ in the ‘backend’ folder.
- Require Express and mongoose at the top of your file:
const express = require(‘express’);
const mongoose = require(‘mongoose’); - Initialize Express and define a port for your server:
const app = express();
const PORT = 5000; - Connect to the MongoDB database using mongoose:
mongoose.connect(‘mongodb://localhost/my-mern-app’, { useNewUrlParser: true, useUnifiedTopology: true }); - Start the server:
app.listen(PORT, () => {
console.log(Server is running on port ${PORT}
);
});
Step 4: Creating Routes
- Create a new folder called ‘routes’ inside the ‘backend’ folder.
- Create a new file called ‘api.js’ inside the ‘routes’ folder.
- Define your API routes in ‘api.js’:
const express = require(‘express’);
const router = express.Router();
router.get(‘/posts’, (req, res) => {
res.send(‘Get all posts’);
});
router.post(‘/posts’, (req, res) => {
res.send(‘Create a new post’);
});
module.exports = router;
- Require and use the routes in your ‘server.js’ file:
const apiRoutes = require(‘./routes/api’);
app.use(‘/api’, apiRoutes);
Step 5: Setting Up React
- Navigate to the ‘src’ folder in your React app.
- Create a new folder called ‘components’.
- Create a new file called ‘Posts.js’ inside the ‘components’ folder.
- Write a simple functional component in ‘Posts.js’:
import React from ‘react’;
const Posts = () => {
return (
Welcome to My MERN App
);
};
export default Posts;
- Import and render this component in your ‘App.js’ file:
import React from ‘react’;
import Posts from ‘./components/Posts’;
function App() {
return (
);
}
export default App;
Step 6: Running the Application
- Run the backend server using the following command in the ‘backend’ folder:
node server.js - Run the React app using the following command in the project directory:
npm start
Congratulations! You have successfully built a web application using the MERN stack. This is just a starting point, and there is much more you can do with these technologies. Keep exploring and building new projects to enhance your skills in web development.
Thank you for following along with our tutorial. Happy coding!