ExpressJS | Manvi Sharma

Post

editor-img
Manvi Sharma
Apr 22, 2023

ExpressJS

Express.js as defined on their website is a fast, opinionated, minimalist web framework

for node.js. It is like a layer built on the top of the node that helps manage a server and routing.

In this post, we will discuss the basic concepts of express js in detail

Why would we want to use express?

Because express is built on top of the node, it is the perfect framework for ultra-fast input

and output.

Node.js is both asynchronous and single-threaded. Meaning many requests can be made simultaneously. Without incurring a bottleneck that would slow down processing.

The robust API that ships with Express allows us to configure routes to send easily and receive requests from a front end.

and connect to a database.

Installing Express.js

In order to install Express.js in your system, we must have Node.js already installed.

To install Express.js, first, you need to create a project directory and create a package.json file that will hold the project dependencies. Below is the code to perform the same:

npm init

Now, you can install the express.js package in your system. To install it globally, you can use the below command:

npm install -g express

Or, if you want to install it locally into your project folder, you need to execute the below command:

npm install express --save

Express.js fundamentals:

Routing and HTTP Methods

Routing refers to the process of determining a specific behavior of an application. It defines the application's response to a client request to a particular route, path, or URI along with a specific HTTP Request method.

Each route can contain multiple handler functions, executed when the user browses for a specific route.

Below is the structure of Routing in Express:

app.METHOD(PATH, HANDLER)

Middleware

Any number of functions invoked by the express js routing layer before the final request is made.

In express, middleware functions are the functions that have access to the request and response objects along with the next function present in the application’s request-response cycle. These functions are capable of performing the below-listed tasks:

Execution of any codeModify the request and the response objects.End applications request-response cycle.Call the next middleware present in the cycle

Cookies

Cookies in Express.js are the small files of information which are stored within a user’s computer. They hold a decent amount of data specific to a particular user and the websites he visits. Each time a client loads a website on his browser, the browser will implicitly send the locally stored data back to the website/server, in order to recognize the user

REST API

REST or RESTful stands for REpresentational State Transfer. It is an architectural style as well as an approach for communications purposes that is often used in various web services development. In simpler terms, it is an application program interface (API) that makes use of HTTP requests to GET, PUT, POST and DELETE the data over WWW.

Scaffolding

While working with REST APIs you must have seen that you need to work with various files such as HTML, CSS, JPEGs, etc., for accomplishing any particular kind of task. But it becomes too much of a hassle to manage and maintain these files.

Thus Express provides the developers with an easy solution to this called Scaffolding.

Scaffolding is the process of creating a proper structure for a web application and helps in managing the files in proper directories saving us from manual work.

Thus, you can say that Scaffolding helps in creating the skeleton for our express application and lets us directly jump into the application development. Express provides various Scaffolding tools such as Yeoman and Express-generator.

Using express-generator

Before you can use express-generator, you need to install this in your system. Type in the below code in the command prompt for the same:

npm install -g express-generator

Now you can use express-generator to create the project skeleton. For that you need to type in the following command:

express

This will now create a skeleton for the express app

Database Connectivity

There are a number of databases available in the market out of which most popular ones among front-end developers are MySQL and MongoDB. Here are the links to connect our database to an express application

Connecting a REST API with MySQL: https://medium.com/edureka/node-js-mysql-tutorial-cef7452f2762

Connecting REST API with MongoDB: https://medium.com/edureka/node-js-mongodb-tutorial-fa80b60fb20c

Templating

Templating engine facilitates us in creating and using the static HTML template with minimal code. When you execute them, the variables in the template file get replaced by actual values, and the entire template is converted into an HTML file by the template engine. Then, it is finally sent to the client side where data is further injected into the actual template to produce the final HTML document.

This reduces the page load time as well as the designing complexity and thus is majorly used for designing HTML pages.

Below I have listed down some of the popular template engines that work well with Express.js:

->Pug (earlier know as Jade)

->mustache

->dust

->handlebars

->hogan

->liquor

->toffee

->underscore

->walrus

->whiskers

Error Handling

Error Handling in Express.js is the mechanism for catching and processing the possible errors which might occur during the program execution and disrupt the normal flow. Express, takes care of errors with the help of a middleware which is further categorized in various types based on the number of arguments the middleware function takes in.

Here, we will be talking specifically about the “error handling middleware” function which takes in four arguments namely, err, req, res and next. This function is invoked only if an error occurs.

app.use(function (err, req, res, next) { console.error(err.stack); res.status(500).send(' <h2 style="font-family: Malgun Gothic; color: darkred;">Ooops... Cant find what you are looking for!</h2> ');});

The above mentioned are all the basic concepts of express js we need to know before starting to work in express js