MERN Stack: A complete MERN guide - Part 3

Nur Islam
14 min readSep 29, 2022

--

This tutorial is all about the MERN stack. We’ll outline the basics of the MERN stack and demonstrate how to use it by developing a simple CRUD application from scratch.

To show how the MERN stack works, we’ll first configure the server side by connecting Node.js and Express.js to MongoDB on the backend. Then, we’ll create some APIs. After that, we’ll walk you through building the front end, using React to build our user interfaces. Once both are complete, we’ll connect the front end to the back end.

Meanwhile, we’ll cover the following topics

  • Building the frontend
  • Setting up Create React App
  • Initial project structure
  • Frontend tasks and features
  • Adding feature components
  • Connecting the frontend and backend
  • Running the frontend and backend
  • Testing our MERN stack app in the browser

This demo is designed to highlight the MERN setup. The objective is to develop a simple project with the best possible structure so that you can use it as a boilerplate and elevate your MERN stack projects to meet industry standards.

Building the frontend

So far, so good! Now that we’ve set up our backend, it’s time to transition to the frontend part of this MERN stack tutorial.

In this section, we’ll use React to build our user interfaces. React is a JavaScript library for building user interfaces. It is maintained by Facebook and a community of individual developers and other companies.

We’ll use Create React App to generate our initial file setup. CRA is a comfortable environment for learning React and is the best way to start building applications in React. It offers a modern build setup with no configuration.

We’ll also use webpack and Babel to bundle our modules and compile our JavaScript, respectively. If you don’t know webpack or Babel well, no problem; you don’t need to install or configure tools like webpack or Babel. They’re preconfigured and hidden so that you can focus on the code. Just create a project, and you’re good to go.

You’ll also need any version of Node.js greater than 8.10 and any version of npm greater than 5.6 installed on your local development machine.

Setting up Create React App

Set any directory using a terminal where you want to keep all the files of this project and run the following command to get the initial setup file:

$ npx create-react-app my-app

You can replace my-app with whatever you'd like to use as your project name. For example, my project name is mern_a_to_z_client, and my command is:

$ npx create-react-app mern_a_to_z_client

Note: The project name must be in lowercase letters.

If everything goes right, then you will see something like the following image, where you will find some instructions along with the commands.

Before using any built-in command, we need to go inside the project folder.

$ cd mern_a_to_z_client

Now that we are in the project directory, we can use those available commands. If you’re using Yarn:

$ yarn start

Or, if using npm:

$ npm start

To run the app in development mode, you can use any of the above commands, and you will see the following message in your terminal.

Now open http://localhost:3000 to view it in the browser. This page will automatically reload if you make changes to the code.

Initial project structure

Inside the project directory, our initial file structure should look like this:

Adding Bootstrap and Font Awesome to your React app

We have got our initial setup file for the front-end part. Now we can start integrating our back end with our front end. Before that, though, I want to add Bootstrap and Font Awesome’s CDN to our project.

Open the file called index.html, which is in the public folder mern_a_to_z_client/public/index.html, and replace everything with the following code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<!-- bootstrap css cdn -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<!-- fontawesome cdn -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<title>MERN A to Z</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
<!-- bootstrap JS cdn -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

Frontend tasks and features

We will work with five different features:

  1. Add, create or save a new book
  2. Show all the books we have stored in the database
  3. Show a single book
  4. Update a book
  5. Delete a book

Dependencies packages installation

Now, use the following command to add some necessary dependencies:

$ npm install --save react-router-dom $ npm install --save axios

Why Axios?

Axios is a lightweight HTTP client based similar to a Fetch API. Axios is a promise-based async/await library for readable asynchronous code. We can easily integrate with React, and it is effortless to use in any front-end framework.

We’ll call our APIs through Axios.

Package.json file

At this point, our package.json file should be similar to this; versions can be similar or different:

// MERN_A_to_Z_Client - package.json{
"name": "mern_a_to_z_client",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.19.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

Creating the component file

Inside the src folder ( mern_a_to_z_client/src/), create another folder called components, and inside it, create five different files:

  1. CreateBook.js
  2. ShowBookList.js
  3. BookCard.js
  4. ShowBookDetails.js
  5. UpdateBookInfo.js

We will work with these five files a bit later.

Setup route

Open the folder called App.js inside the src folder ( mern_a_to_z_client/src/App.js), and replace it with the following code:

import React, { Component } from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import './App.css';
import CreateBook from './components/CreateBook';
import ShowBookList from './components/ShowBookList';
import ShowBookDetails from './components/ShowBookDetails';
import UpdateBookInfo from './components/UpdateBookInfo';
class App extends Component {
render() {
return (
<Router>
<div>
<Route exact path='/' component={ShowBookList} />
<Route path='/create-book' component={CreateBook} />
<Route path='/edit-book/:id' component={UpdateBookInfo} />
<Route path='/show-book/:id' component={ShowBookDetails} />
</div>
</Router>
);
}
}
export default App;

Here, we define all the routes. For a specific path definition, its corresponding component will be rendered. We have not implemented these files/components yet — just completed the path setup.

Updating the CSS file

Update a CSS file called App.css in the src folder with the following code:

.App {
text-align: center;
}
.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 40vmin;
pointer-events: none;
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.CreateBook {
background-color: #2c3e50;
min-height: 100vh;
color: white;
}
.ShowBookDetails {
background-color: #2c3e50;
min-height: 100vh;
color: white;
}
.UpdateBookInfo {
background-color: #2c3e50;
min-height: 100vh;
color: white;
}
.ShowBookList {
background-color: #2c3e50;
height: 100%;
width: 100%;
min-height: 100vh;
min-width: 100px;
color: white;
}
/* BookList Styles */
.list {
display: grid;
margin: 20px 0 50px 0;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 1fr;
grid-gap: 2em;
}
.card-container {
width: 250px;
border: 1px solid rgba(0,0,.125);
margin: 0 auto;
border-radius: 5px;
overflow: hidden;
}
.desc {
height: 130px;
padding: 10px;
}
.desc h2 {
font-size: 1em;
font-weight: 400;
}
.desc h3, p {
font-weight: 300;
}
.desc h3 {
color: #6c757d;
font-size: 1em;
padding: 10px 0 10px 0;
}

Adding our feature components

Now it’s time to add feature components to our MERN stack project.

Create a new book

Our CreateBook.js file is responsible for adding, creating, or saving a new book or a book's info. So, update CreateBook.js with the following code:

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import '../App.css';
import axios from 'axios';
class CreateBook extends Component {
constructor() {
super();
this.state = {
title: '',
isbn:'',
author:'',
description:'',
published_date:'',
publisher:''
};
}
onChange = e => {
this.setState({ [e.target.name]: e.target.value });
};
onSubmit = e => {
e.preventDefault();
const data = {
title: this.state.title,
isbn: this.state.isbn,
author: this.state.author,
description: this.state.description,
published_date: this.state.published_date,
publisher: this.state.publisher
};
axios
.post('http://localhost:8082/api/books', data)
.then(res => {
this.setState({
title: '',
isbn:'',
author:'',
description:'',
published_date:'',
publisher:''
})
this.props.history.push('/');
})
.catch(err => {
console.log("Error in CreateBook!");
})
};
render() {
return (
<div className="CreateBook">
<div className="container">
<div className="row">
<div className="col-md-8 m-auto">
<br />
<Link to="/" className="btn btn-outline-warning float-left">
Show BooK List
</Link>
</div>
<div className="col-md-8 m-auto">
<h1 className="display-4 text-center">Add Book</h1>
<p className="lead text-center">
Create new book
</p>
<form noValidate onSubmit={this.onSubmit}>
<div className='form-group'>
<input
type='text'
placeholder='Title of the Book'
name='title'
className='form-control'
value={this.state.title}
onChange={this.onChange}
/>
</div>
<br />
<div className='form-group'>
<input
type='text'
placeholder='ISBN'
name='isbn'
className='form-control'
value={this.state.isbn}
onChange={this.onChange}
/>
</div>
<div className='form-group'>
<input
type='text'
placeholder='Author'
name='author'
className='form-control'
value={this.state.author}
onChange={this.onChange}
/>
</div>
<div className='form-group'>
<input
type='text'
placeholder='Describe this book'
name='description'
className='form-control'
value={this.state.description}
onChange={this.onChange}
/>
</div>
<div className='form-group'>
<input
type='date'
placeholder='published_date'
name='published_date'
className='form-control'
value={this.state.published_date}
onChange={this.onChange}
/>
</div>
<div className='form-group'>
<input
type='text'
placeholder='Publisher of this Book'
name='publisher'
className='form-control'
value={this.state.publisher}
onChange={this.onChange}
/>
</div>
<input
type="submit"
className="btn btn-outline-warning btn-block mt-4"
/>
</form>
</div>
</div>
</div>
</div>
);
}
}
export default CreateBook;

Show all books

The ShowBookList.js component will be responsible for showing all the books we already have stored in our database. Update ShowBookList.js with this code:

import React, { Component } from 'react';
import '../App.css';
import axios from 'axios';
import { Link } from 'react-router-dom';
import BookCard from './BookCard';
class ShowBookList extends Component {
constructor(props) {
super(props);
this.state = {
books: []
};
}
componentDidMount() {
axios
.get('http://localhost:8082/api/books')
.then(res => {
this.setState({
books: res.data
})
})
.catch(err =>{
console.log('Error from ShowBookList');
})
};
render() {
const books = this.state.books;
console.log("PrintBook: " + books);
let bookList;
if(!books) {
bookList = "there is no book record!";
} else {
bookList = books.map((book, k) =>
<BookCard book={book} key={k} />
);
}
return (
<div className="ShowBookList">
<div className="container">
<div className="row">
<div className="col-md-12">
<br />
<h2 className="display-4 text-center">Books List</h2>
</div>
<div className="col-md-11">
<Link to="/create-book" className="btn btn-outline-warning float-right">
+ Add New Book
</Link>
<br />
<br />
<hr />
</div>
</div><div className="list">
{bookList}
</div>
</div>
</div>
);
}
}
export default ShowBookList;

Creating a card for each book

Here we use a functional component called BookCard.js, which takes a book's info from ShowBookList.js and makes a card for each book. Write the following code to update your BookCard.js file:

import React from 'react';
import { Link } from 'react-router-dom';
import '../App.css';
const BookCard = (props) => {
const book = props.book;
return(
<div className="card-container">
<img src="https://commapress.co.uk/books/the-book-of-cairo/cairo-provisional-v3/image%2Fspan3" alt="" />
<div className="desc">
<h2>
<Link to={`/show-book/${book._id}`}>
{ book.title }
</Link>
</h2>
<h3>{book.author}</h3>
<p>{book.description}</p>
</div>
</div>
)
};
export default BookCard;

NOTE: Here, I used the same img src for each book, since each book's respective image may not always be available. Change the image source, and you can also use a different image for each book.

Show a book’s info

The ShowBookDetails component has one task: it shows all the info we have about any book. We have both delete and edit buttons here to get access.

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import '../App.css';
import axios from 'axios';
class showBookDetails extends Component {
constructor(props) {
super(props);
this.state = {
book: {}
};
}
componentDidMount() {
// console.log("Print id: " + this.props.match.params.id);
axios
.get('http://localhost:8082/api/books/'+this.props.match.params.id)
.then(res => {
// console.log("Print-showBookDetails-API-response: " + res.data);
this.setState({
book: res.data
})
})
.catch(err => {
console.log("Error from ShowBookDetails");
})
};
onDeleteClick (id) {
axios
.delete('http://localhost:8082/api/books/'+id)
.then(res => {
this.props.history.push("/");
})
.catch(err => {
console.log("Error form ShowBookDetails_deleteClick");
})
};
render() {const book = this.state.book;
let BookItem = <div>
<table className="table table-hover table-dark">
{/* <thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead> */}
<tbody>
<tr>
<th scope="row">1</th>
<td>Title</td>
<td>{ book.title }</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Author</td>
<td>{ book.author }</td>
</tr>
<tr>
<th scope="row">3</th>
<td>ISBN</td>
<td>{ book.isbn }</td>
</tr>
<tr>
<th scope="row">4</th>
<td>Publisher</td>
<td>{ book.publisher }</td>
</tr>
<tr>
<th scope="row">5</th>
<td>Published Date</td>
<td>{ book.published_date }</td>
</tr>
<tr>
<th scope="row">6</th>
<td>Description</td>
<td>{ book.description }</td>
</tr>
</tbody>
</table>
</div>
return (
<div className="ShowBookDetails">
<div className="container">
<div className="row">
<div className="col-md-10 m-auto">
<br /> <br />
<Link to="/" className="btn btn-outline-warning float-left">
Show Book List
</Link>
</div>
<br />
<div className="col-md-8 m-auto">
<h1 className="display-4 text-center">Book's Record</h1>
<p className="lead text-center">
View Book's Info
</p>
<hr /> <br />
</div>
</div>
<div>
{ BookItem }
</div>
<div className="row">
<div className="col-md-6">
<button type="button" className="btn btn-outline-danger btn-lg btn-block" onClick={this.onDeleteClick.bind(this,book._id)}>Delete Book</button><br />
</div>
<div className="col-md-6">
<Link to={`/edit-book/${book._id}`} className="btn btn-outline-info btn-lg btn-block">
Edit Book
</Link>
<br />
</div>
</div>
{/* <br />
<button type="button" class="btn btn-outline-info btn-lg btn-block">Edit Book</button>
<button type="button" class="btn btn-outline-danger btn-lg btn-block">Delete Book</button> */}
</div>
</div>
);
}
}
export default showBookDetails;

Update a book’s info

UpdateBookInfo.js, as its name indicates, is responsible for updating a book's info. An Edit Book button will trigger this component to perform. After clicking Edit Book, we will see a form with the old info, which we will be able to edit or replace.

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import axios from 'axios';
import '../App.css';
class UpdateBookInfo extends Component {
constructor(props) {
super(props);
this.state = {
title: '',
isbn: '',
author: '',
description: '',
published_date: '',
publisher: ''
};
}
componentDidMount() {
// console.log("Print id: " + this.props.match.params.id);
axios
.get('http://localhost:8082/api/books/'+this.props.match.params.id)
.then(res => {
// this.setState({...this.state, book: res.data})
this.setState({
title: res.data.title,
isbn: res.data.isbn,
author: res.data.author,
description: res.data.description,
published_date: res.data.published_date,
publisher: res.data.publisher
})
})
.catch(err => {
console.log("Error from UpdateBookInfo");
})
};
onChange = e => {
this.setState({ [e.target.name]: e.target.value });
};
onSubmit = e => {
e.preventDefault();
const data = {
title: this.state.title,
isbn: this.state.isbn,
author: this.state.author,
description: this.state.description,
published_date: this.state.published_date,
publisher: this.state.publisher
};
axios
.put('http://localhost:8082/api/books/'+this.props.match.params.id, data)
.then(res => {
this.props.history.push('/show-book/'+this.props.match.params.id);
})
.catch(err => {
console.log("Error in UpdateBookInfo!");
})
};
render() {
return (
<div className="UpdateBookInfo">
<div className="container">
<div className="row">
<div className="col-md-8 m-auto">
<br />
<Link to="/" className="btn btn-outline-warning float-left">
Show BooK List
</Link>
</div>
<div className="col-md-8 m-auto">
<h1 className="display-4 text-center">Edit Book</h1>
<p className="lead text-center">
Update Book's Info
</p>
</div>
</div>
<div className="col-md-8 m-auto">
<form noValidate onSubmit={this.onSubmit}>
<div className='form-group'>
<label htmlFor="title">Title</label>
<input
type='text'
placeholder='Title of the Book'
name='title'
className='form-control'
value={this.state.title}
onChange={this.onChange}
/>
</div>
<br />
<div className='form-group'>
<label htmlFor="isbn">ISBN</label>
<input
type='text'
placeholder='ISBN'
name='isbn'
className='form-control'
value={this.state.isbn}
onChange={this.onChange}
/>
</div>
<div className='form-group'>
<label htmlFor="author">Author</label>
<input
type='text'
placeholder='Author'
name='author'
className='form-control'
value={this.state.author}
onChange={this.onChange}
/>
</div>
<div className='form-group'>
<label htmlFor="description">Description</label>
<input
type='text'
placeholder='Describe this book'
name='description'
className='form-control'
value={this.state.description}
onChange={this.onChange}
/>
</div>
<div className='form-group'>
<label htmlFor="published_date">Published Date</label>
<input
type='date'
placeholder='published_date'
name='published_date'
className='form-control'
value={this.state.published_date}
onChange={this.onChange}
/>
</div>
<div className='form-group'>
<label htmlFor="publisher">Publisher</label>
<input
type='text'
placeholder='Publisher of this Book'
name='publisher'
className='form-control'
value={this.state.publisher}
onChange={this.onChange}
/>
</div>
<button type="submit" className="btn btn-outline-info btn-lg btn-block">Update Book</button>
</form>
</div>
</div>
</div>
);
}
}
export default UpdateBookInfo;

Connecting the frontend to the backend

We just implemented all of our components! Now we need a little change in our server-side (back-end) project.

Changes required on the backend

If we try to call our back-end API from the front-end part, it gets an error that says: “Access to XMLHttpRequest at ‘ http://localhost:8082/api/books&#8217; from origin ‘ http://localhost:3000&#8217; has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.”

To solve this, we need to install cors in our back-end (server-side) project. Go to the project folder (e.g., MERN_A_to_Z) and run:

$ npm install cors

Now, update app.js (the back end's entry point) with the following code:

// app.jsconst express = require('express');
const connectDB = require('./config/db');
var cors = require('cors');
// routes
const books = require('./routes/api/books');
const app = express();// Connect Database
connectDB();
// cors
app.use(cors({ origin: true, credentials: true }));
// Init Middleware
app.use(express.json({ extended: false }));
app.get('/', (req, res) => res.send('Hello world!'));// use Routes
app.use('/api/books', books);
const port = process.env.PORT || 8082;app.listen(port, () => console.log(`Server running on port ${port}`));

Running the frontend and backend

Follow the steps below to run both the front end and backend of our MERN stack example.

Run the server

Now, run the server (inside the project folder):

$ npm run app

If you get any errors, then follow the commands below (inside the project folder):

$ npm install 
$ npm run app

Run the client

From the front-end project directory, run the command below:

$ npm start

If you get an error, again, follow the same commands below:

$ npm install 
$ npm start

Testing our MERN stack app in the browser

Let’s check everything in the browser. Open http://localhost:3000 in your browser. Now you can add a book, delete a book, show the list of books, and edit books. The following routes should perform accordingly:

Add a new book: http://localhost:3000/create-book

Show the list of books: http://localhost:3000/

Show any book’s info: http://localhost:3000/show-book/:id

Update a book’s info: http://localhost:3000/edit-book/:id

Congratulations! You have successfully completed this MERN stack tutorial.

Read part 1 here and part 2 here.

You can visit my GitHub to see both the server-side and client-side portions of this MERN stack tutorial. You can also find the complete repo for our MERN stack example app on GitHub.

Originally published at https://blog.logrocket.com on February 26, 2021.

--

--