This project is for understanding the fundamentals of a MERN application. In this README.md file I have listed all the steps I took to build a very simple MERN application which can perfrom CRUD operations.
-
Created server.js file and wrote
console.log("Hello from Node.js");
-
Opened the terminal and wrote:
node server.js
--> that gave us the output -
wrote:
npm init -y
--> That created a package.json which will keep track of all dependencies -
Install Express.js -->
npm install express
-
After setting up basic web server if we go to terminal and type:
node server.js
then our teminal won't stop running -
Now in the browser if we go to
localhost:3000
we will see our welcome message -
If we go to
localhost:3000/admin
then we will see the welcome message for admins -
So this is already the "E" and "N" of the "MERN" stack
-
We would use MongoDB locally on our machine
-
Open MongoDB Compass and click on
Connect
without giving any connection string to it. This will show us some pre-built databases which we can avoid -
We can create our database from the
+
sign below and name it asBasicMernApp
and we can also give it a collection. Let's name it ascats
then we can add some data to that collection -
To connect MongoDB to the app we need to install MongoDB driver:
npm install mongodb
-
When we are working locally with MongoDB the connection string will be
mongodb://localhost:27017/DB_NAME
-
We need to restart the server for every changes that we make. It's a tidious task. So to automate this we need to
npm install nodemon
This will automatically restart the server with each change that we make -
To setup nodemon we need to go to
package.json
and insidescripts
we need to type the name of our server and necessary command to run that. For example we can type:"ourserver": "nodemon server.js"
-
Now we can just go to the terminal and type
npm run ourserver
and then nodemon will automatically restart the server for us anytime we save a change -
To get the ejs template engine we need to go to terminal and type:
npm install ejs
-
To setup React for client side we need a bunch of packages to install. So go to terminal and type:
npm install react react-dom @babel/core @babel/preset-react babel-loader webpack webpack-cli webpack-node-externals npm-run-all
-
Among all these packages
webpack
is the tool that converts React syntax to plain JavaScript -
How do we tell webpack to do what we need to do? --> We need to create
webpack.config.js
file in the root directory. And by pasting some boilerplate code there will do the job -
Just like
nodemon
tracks changes into our server-side file and restarts the server whenever there is a change, thewebpack
will also monitor our client-side file and retranspile the browser whenever there is a change -
To setup webpack we need to go to
package.json
and insidescripts
we need to type the name of our webpack and necessary command to run that. For example we can type:"ourwebpack": "webpack --watch"
-
Instead of running our client side and server side separately we can establish a command which can run them both at once. We need to go to the
scripts
section of thepackage.json
file and type:"project": "run-p ourserver ourwebpack"
-
Now we can just go to the terminal and type
npm run project
and then both nodemon and webpack will start running -
To fetch and send data from React application we can go to terminal and type:
npm install axios
-
Link the CSS stylesheet from
Bootstrap
in theadmin.ejs
file -
Create a new file
my-custom.css
in thepublic
folder and attach it to theadmin.ejs
-
Style the UI of admin page using React in two files
CatCard.js
andCreateNewForm.js
-
Whenever we need to send files to the database we need to use a package named
Multer
To install it go to the terminal and type:npm install multer
-
To cleanup data we need a package called
Sanitize HTML
To install it go to the terminal and type:npm install sanitize-html
-
To upload images we need to instruct the system to make a folder into the
public
directory and resize the image before storing there. To do so we will be needing two packages calledfs-extra
andsharp
-
fs-extra
is a package to manage files and folders in Node app andsharp
is a package to resize images. To install them go to the terminal and type:npm install fs-extra sharp
-
When the CRUD operation is done to show our fetched data on a server generated HTML page we can render React code on the server side. We can do so by stoping the terminal from running and then go to
package.json
file and changing thenodemon server.js
tonodemon server-compiled