What is the MERN stack?
A stack is a set of tools that work together, like the parts of a bicycle. MERN has four parts.
- MongoDB is a database. A database is a place that stores your data in an organized way.
- Express is a small tool that receives requests from visitors and sends back answers.
- React builds the pages people see in their browser.
- Node.js lets you run JavaScript on a server, not just in a browser.
Before you start
You need a VPS (a virtual private server, which is your own slice of a bigger computer) running Ubuntu. Log in with SSH. SSH is a safe way to type commands on a remote computer. You also need MongoDB running. Install it on the server, or ask Hostvento support what is available on your plan.
Steps
- Update the server package list.
This command refreshes the list of software you can install.apt update - Install Node.js and npm. The npm tool downloads add-on code for Node.js.
apt install -y nodejs npm - Check that both work.
Each command prints a version number.node -v npm -v - Make a folder for the back end (the part visitors never see) and go into it.
mkdir -p ~/mern-app/server cd ~/mern-app/server - Start a project and install Express and Mongoose. Mongoose helps Node.js talk to MongoDB.
npm init -y npm install express mongoose cors - Create a file named
server.jswith this content.const express = require('express'); const mongoose = require('mongoose'); const app = express(); mongoose.connect('mongodb://127.0.0.1:27017/myapp'); app.get('/api/hello', (req, res) => res.json({ message: 'Hello from Express' })); app.listen(5000, () => console.log('Server running on port 5000')); - Run the back end.
It should print "Server running on port 5000".node server.js - Open a second terminal. Make the front end with React.
This builds a starter React page and runs it.cd ~/mern-app npm create vite@latest client -- --template react cd client npm install npm run dev
Tip: Do not leave the database open to the whole internet. Keep MongoDB listening only on
127.0.0.1 unless you know why you need more.Quick recap
- MERN means MongoDB, Express, React and Node.js.
- Install Node.js first, then create the back end with Express.
- Connect the back end to MongoDB with Mongoose.
- Create the front end with React.
- Keep your database private.