Express for beginners
Tags: node, javascript, webdev, programming
Published: 2022-01-13 16:01:07

**What is Express:** Express in a node js framework which provides huge features to build web applications. Based on a different route, URL or HTTP call it provides different calls or actions. Besides that, it gives a middleware facility to respond to HTTP requests.

## Installation process:

**Node install:** Install node.js from windows downloading software and install it. Otherwise can install through chocolatey which is the command-line package manager and installer for Windows software.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plwviq5zxcod9keiqe2a.png)

If already installed you can upgrade node.js to its latest version to get the latest features and support.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2y10ap6he6i5tm35wttc.png)

**Nodemon install:** Install NODEMON globally.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6f8qk1hb9h27qrj1vvav.png)

## Project Creation Steps:

**Create Project:** Make a new directory for you server-side project.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wm56nwgtlco1w4yu2al.png)

**Change directory:** Go to the new created directory using it’s given name.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mci3btyuze01jf3zzi4.png)

**Create package:** Create environment for project package files.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/seifitto47ilhwdwct6z.png)

**Install dependencies:** For MERN projects install initial all dependencies.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9r4dl5hhokuqmerq0nap.png)

**Add dependency:** Add package.json scripts dependency if any are missing.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06khl6onbpyk284ktcbm.png)

**Express Run:** Create a file name index.js. Then require express, app and set port at process.env.PORT or 5000. Check the server by request and listen to the response at the command line.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ypryg42jo0qzq7vcomli.png)

## MongoDb setup:

**Set initial info:** To avoid any port connection error require cors. To secure your mongodb username, password, firebase private key create a .env file and require dotenv. For using mongodb requires MongoClient. For removing any cors error user cors and get data on json format use express.json().


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bj6puzpa0pfh5pc3s4h9.png)

**.env file:** Set mongodb network access and set IP address. Create database access by creating username and password. Now create .env file to keep username and password securely.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/os0g76z5c3tnl1gj5qdj.png)

**Connect MongoDB:** Add mongo connection uri link and convert username and password by template literal. Also connect with the client using the async await function.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dbdvl1odzs0npflrze7.png)

**Set Database collection:** Depending on different header requests create different API route into try block and apply different actions depending on client api request. Create database and database collection for specific data.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5jx2n8yxkmzl1bcoz6qj.png)

## CRUD Operation:

**Create/POST request:** For header post request from client-side to server-side when post route matches. When the request matches the action occurred like inserting the post data into the database.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8rtb39xbu045pwckhvv.png)

Here, insertOne() inserting the data to the database collection.

**Read/GET request:** For header get a request from client-side to server-side when getting route match. When the request matches the action occurred like getting the data from the database.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4n3hli698csnnxeql0r.png)

Here, find() will search or find the data to the database collection.

**PUT request:** For header put or update request from client-side to server-side when getting route match. When the request matches the action occurs like if the information is available then updates into the database. otherwise if not available then insert into the database.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06itodtpeq6zhwzhgoty.png)

Here, filter finding the specific id, options indicate is upserted true or false and updateDoc() gets the updated data. Finally updateOne updates data to the database collection.

**DELETE request:** For header delete request from client-side from server-side when getting route match. When the request matches the action occurred like deleting the information from the database.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l41y8mqthdjoxf3pgiec.png)

Here, deleteOne() deletes the specific data from the database collection.