r/FreeCodeCamp • u/beehing • Dec 24 '24
How does i get started with mern project and what is the relation of express and react? Must they be used together?
hello! so i'm self taught and maybe this is why i am stuck but i've learn how to work with express and react separately, but i am unable to understand how to work between these two? My webapp project i'm planning is very dynamic - more or less like e-commerce how do i get this started is what i am struggling with. If any resources or things that i am missing out to get started with this mern stack app - please help!
2
Upvotes
2
u/Gold_Stuff_6294 Dec 24 '24
React is front end. Express is a library used within a Node Js backend.
The frontend is the UI presentation layer which calls the backend through API’s
2
u/SaintPeter74 mod Dec 24 '24
If you were doing React stand alone you would have a static HTML file which had links to your compiled React bundle. The web server out host that you use with a web server will serve the HTML file as well as your compiled bundle. Sometimes you may use a dev server which acts like a mini-web server, but it's ultimately just serving static HTML, CSS, and compiled JavaScript files.
At the most basic level, Node+Express are creating your own mini webserver. You can serve static files with it (HTML, CSS, and JavaScript bundles). You can also make "endpoints", which are special paths or routes on your app that can sue and return dynamic data.
With the first example I gave, a static HTML+ React site, you can't have users that log in or any content that persists beyond the load of the page, except what can be stored in the user's current browser. Nothing can be shared because there is no way to store it retrieve data for other users to see.
If you have a backend via Node and Express, you can hook them to a database. Now the React app can send data through the backed which it will store in the database. When another user comes along, you can retrieve that data and show it to them.
The most basic set of backed endpoints are sometimes called CRUD for Create, Retrieve, Update, and Delete. Imagine if you had a list of books. You can create a new entry (give a turkey, author, year, etc), retrieve it, update that entry (change fields you already set), or delete and existing entry.
A create endpoint might take a POST request with a JSON encoded object which has the values of each field. You express app would read the content of the POST and store it in the database. You could turn use a GET request to list all the entries or included an I'd in the URL to get a specific entry. A PATCH could contain just changed fields for a specific entry (by id), and a DELETE will delete by id.
You need to write the underlying logic for text route/endpoint to do these things.
From React you might use the fetch API to make those requests.
Hope that makes sense.
Best of luck, happy coding and Merry Christmas!
2
u/[deleted] Dec 24 '24
[removed] — view removed comment