hitps:fmedum.com/@nonamedev/bulding-ful-stack-appications-w it-mern-stack-a-comprehensive-guide-402e98d2e963 33y9iri24, 21:58 MERN Stack | Mecium
ds
}
export default App;
4. Run the command npm start in the client folder to start the React development
server.
5. Visit http: //localhost:3000/ in your web browser to see the React application
running. You should see the message “Hello World!” displayed on the screen.
‘Connecting the Frontend and Backend
Now that we have built the frontend and backend of our application, we need to
connect them. We will use the Axios library to make HTTP requests to the backend
from the frontend. Follow these steps to connect the frontend and backend:
1. Install Axios by running the command npn instatt axios inthe client folder.
2. Open the ctient/sre/Aapp. js file and add the following code at the top to import
Axios
import axios from ‘axios';
3. Add the following code to the app component to make an HT'TP request to the
backend
‘import React, { useState, useEffect } from 'react';
‘import axios from ‘axios';
function App()
const [message, setMessage] = useState('');
useEffect(() => {
axios.get('/api')
-then(res => setMessage(res.data))
scatch(err => console. log(err)) 5
3, 3
return (
hitps:fmedum.com/@nonamedev/bulding-ful-stack-applications-w it-mern-stack-a-comprehensive-guide-402e98d2e963 ansy9iri24, 21:58 MERN Stack | Mecium
{message}
3
+
export default App;
4, Modify the server. js file to define the API route and return a message
const express = require('express") 5
const mongoose = require(*mongoose');
const app = express();
// Mongod8 connection
mongoose. connect ('mongodb: //localhost/myapp', { useNewUrlParser: true, useUnifiedTc
-then(() => console. log('MongoDB connected. ..'))
-catch(err => console. log(err)) ;
// Define APL route
app.get('/api', (req, res) => {
res.send('HeLlo from the API!');
Ds
// Serve static assets if in production
if (process.env.NODE_ENV === 'production’) ¢
// Set static folder
app. use (express. static('client/build")) ;
app.get("#', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
ys
// Start the server
const port = process.env.PORT || 5000;
app.Listen(port, () => console. log(*Server started on port ${port}...°))5
5. Restart the servers by stopping the React development server and the Node,js
server and running apm start inthe client folder and nodenon server.js in the
project folder.
-nips:tmedum com/@nonamedevibulding-tul-stack-applications-w th-mern-stack-a-comprehensive-guide-402690d26959 53teins 2188 MER Stack Meu
6. Visit http: //Locathost:3000/ in your web browser to see the message "Hello from
the API!" displayed on the screen.
Conclusion
In this article, we have explored the MERN Stack in detail and built a full-stack
application using the stack. We started by setting up the environment, building the
backend using Node,js, Express.js, and MongoDB, building the frontend using React,
and connecting the frontend and backend using Axios. By following this tutorial, you
should have a good understanding of how to use the MERN Stack to build full-stack
applications.
Mern Stack Full Stack Developer
Written by NONameDev
23 Followers
Living in various forms and interesting new technology.
More from NoNameDev
nips:tmedum com/@nonamedevibulding-tul-stack-applications-w th-mern-stack-a-comprehensive-guide-402690d26959 ens9/724, 21:58 MERN Stack | Mecium
NoNameDev
Natural Language Processing with Python: A Beginner’s Guide with
Example Code and Output
Introduction
‘Apr 25, 2023
NoNameDev
Explainability in Deep Lear Demystifying the Black Box
Deep learning models have achieved outstanding results in numerous fields, such as image
recognition, natural language processing, and...
hitps:fmedum.com/@nonamedevibulding-ful-stack-appiations-w it-mern-stack-a-comprehensive-guide-402e98d2e963 739/724, 21:58 MERN Stack | Mecium
Jul 16, 2023
NoNameDev
Deploying a MERN Stack Application with Docker and Kubernetes: A
Complete Guide
In the world of web development, the MERN stack (MongoDB, Express.js, React, and Node.js)
has gained tremendous popularity. Paired with...
‘Apr 21,2008 tf
nips:tmedum com/@nonamedevibulding-tul-stack-applications-w th-mern-stack-a-comprehensive-guide-402690d26959 anss9iri24, 21:58 MERN Stack | Mecium
NoNameDev
Advanced Django Techniques: Custom Authentication, Middleware, and
Caching for High-Performance Web..
Django is a popular Python web framework that is known for its ease of use and robustness. It
provides a rich set of tools and features for.
Mar 28,2023 a
C See all from NoNameDev +»)
Recommended from Medium
‘Alexander Nguyen in Level Up Coding
The resume that got a software engineer a $300,000 job at Google.
+-page. Well-formatted.
+ unt
hitps:fmedium.com/@nonamedev/bulding-ful-stack-appications-w it-mern-stack-a-comprehensive-guide-402e98d2e963
ons9/724, 21:58 MERN Stack | Mecium
Dominik Polzer in Towards Data Science
17 (Advanced) RAG Techniques to Turn Your LLM App Prototype into a
Production-Ready Solution
Acollection of RAG techniques to help you develop your RAG app into something robust that
will last
+ na tt
Lists
Staff Picks
689 stories - 1139 saves
Stories to Help You Level-Up at Work
19 stories - 692 saves
Self-Improvement 101
20 stories - 2316 saves
Productivity 101
20 stories . 2039 saves
-nips:tmedum com/@nonamedevibulding-tul-stack-applications-w th-mern-stack-a-comprehensive-guide-402690d26959 10139/724, 21:58 MERN Stack | Mecium
John Gorman
Stop Wasting Your Time
ASimple Framework for Making Better Decisions
+ una ti
Benedict Neo in bitgrit Data Science Publication
Roadmap to Learn Al in 2024
A free curriculum for hackers and programmers to learn Al
Mar 11 tt
nips:tmedum com/@nonamedevibulding-tul-stack-applications-w th-mern-stack-a-comprehensive-guide-402690d26959 nsy9iri24, 21:58 [MERN Stack | Mecium
Abhay Parashar in The Pythoneers
17 Mindblowing Python Automation Scripts | Use Everyday
Scripts That Increased My Productivity and Performance
a
+ 4dago
‘Afan Khan in JavaScript in Plain English
Microsoft is ditching React
Here's why Microsoft considers React a mistake for Edge.
nips:tmedum com/@nonamedevibulding-tul-stack-applications-w th-mern-stack-a-comprehensive-guide-402690d26959 sans9/724, 21:58 MERN Stack | Mecium
+ Juné tt
C ‘See more recommendations )
-nips:tmedum com/@nonamedevibulding-tul-stack-applications-w th-mern-stack-a-comprehensive-guide-402690d26959 1983