Express for beginners

Express for beginners

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.

21.png

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

22.png

Nodemon install: Install nodemon globally.

23.png

Project Creation Steps:

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

24.png

Change directory: Go to the new created directory using its given name.

25.png

Create package: Create an environment for project package files.

26.png

Install dependencies: For MERN projects install initial all dependencies.

27.png

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

28.png

Express Run: Create a file name index.js. Then require an 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.

29.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. Using MongoDB requires MongoClient. For removing any cors error user cors and get data on JSON format use express.json().

30.png

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

31.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.

32.png

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

33.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.

34.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.

35.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.

36.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.

37.png

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