Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Get Started with the MERN Stack on Your Server

This guide shows you the pieces of the MERN stack and how to set up a tiny working project on a VPS. You need root access to your server.

VPS and Cloud Servers2 min read8 steps

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

  1. Update the server package list.
    apt update
    This command refreshes the list of software you can install.
  2. Install Node.js and npm. The npm tool downloads add-on code for Node.js.
    apt install -y nodejs npm
  3. Check that both work.
    node -v
    npm -v
    Each command prints a version number.
  4. 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
  5. Start a project and install Express and Mongoose. Mongoose helps Node.js talk to MongoDB.
    npm init -y
    npm install express mongoose cors
  6. Create a file named server.js with 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'));
  7. Run the back end.
    node server.js
    It should print "Server running on port 5000".
  8. Open a second terminal. Make the front end with React.
    cd ~/mern-app
    npm create vite@latest client -- --template react
    cd client
    npm install
    npm run dev
    This builds a starter React page and runs it.
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.