What is MERN?

1. Introduction

  • MERN stands for MongoDB, Express, React, and Node.

2. Big Picture for the MERN Architecture

  • Client (Browser):
    • ReactJS: Responsible for presentation and the user interface.
    • NodeJS & Express: Server-side responsible for business logic and file storage.
    • The server side and client side communicate with requests and responses in JSON format.
  • Database:
    • MongoDB: Responsible for persistent data storage.
    • NodeJS & Express communicate with MongoDB through database queries using the MongoDB SDK.

3. Diving into Client Side

  • React single-page application is served from the server to the browser.
  • React handles rendering everything in the browser.
  • React manages frontend or client-side routing with an extra library, react-router-dom.
  • react-router-dom helps render different React components based on the path the user enters into the URL bar of the browser.
  • The React application typically handles some front-end state that influences what is shown on the screen.
  • Developers build React components and style them.
  • React route configuration defines which React components are rendered for which path.
  • Implement state management with React hooks or Redux.

4. Understanding the Server Side

  • Decoupled Ends:
    • Frontend powered by React.
    • Backend powered by Node Express.
    • Database server powered by MongoDB.
  • Backend:
    • Built as an API (Application Programming Interface).
    • Uses a REST API (Representational State Transfer).
    • APIs perform various actions on the server, such as storing data in a database, validating user input, and retrieving data from a database.
    • REST API uses a combination of different URLs and HTTP verbs (GET, POST, PATCH, DELETE) to build endpoints that trigger different actions when a client makes a request.
    • The REST API executes code on the server.
    • The Node Express server communicates with the database.

5. More About REST

  • Client-Server Combination:
    • The server uses Node Express, which communicates with MongoDB, and the client uses React.
    • A request is a combination of a specific URL, for example, www.yourpage.com/user.
    • The URL combined with a specific HTTP verb triggers a specific action.

REST & HTTP Methods (HTTP Verbs)

  • GET: Retrieve a resource from the server.
  • POST: Create or append a resource on the server.
  • PUT: Create or overwrite a resource on the server.
  • PATCH: Update parts of an existing resource on the server.
  • DELETE: Delete a resource on the server.
  • OPTIONS: Determine whether follow-up requests are allowed (sent automatically).

6. Connecting Node & React

6.1 One Server Hosts Node API & React SPA

  • One server hosts both the Node API and the React single-page application under the same domain.
  • For example, mypage.com/ might return the single-page application, while mypage.com/api serves the endpoints for the React single-page application to communicate with.
  • The Node Express API handles incoming requests on the server. Requests targeting mypage.com/api reach the API, and other requests return the React single-page application.
  • Data is exchanged between the React app and the Node API in JSON format.

6.2 Two Separate Servers

  • One server serves the single React.js application HTML page.
  • Another server hosts the Node (Express) API, which handles incoming requests.
  • Data is exchanged between the React app and the Node API in JSON format.

6.3 MongoDB Database

  • MongoDB always has its own database server. This server could be installed on the same physical machine as the Node and React app.

Comments

Popular posts from this blog

WSL2配置proxychains, 使得终端走宿主机代理

Javascript Currying

Node.js & Express.js - A Refresher