Express.js: Features & Fundamental Concepts

Rachel Andersen
6 min readAug 23, 2021

What is Express.js?

Express is a web application framework for Node.js that works on the back-end to provide features for both mobile and web applications. It was created by T.J. Holowaychuk in 2010 and it is free and open-source. It is favored by developers because it is easy to learn since it only requires knowledge of JavaScript and HTML. Some well-known applications to utilize Express.js are Twitter, GoDaddy, and PayPal.

Features of Express.js

  1. Express.js offers faster server-side development and quickens the development pace of an application. Since Express.js provides many commonly used features of Node.js in the form of functions that can be used anywhere in the program, it removes the need to code for a long time by cutting coding time by about half.
  2. It can work with various templating engines: Pug, Mustache, and EJS.
  3. It follows MVC (Model-View-Controller) architecture.
  4. It makes the integration process with databases effortless.
  5. It defines an error-handling middleware. Middleware is the part of the program that has access to the database, client requests, and other middleware. Middleware is responsible for the organization of different functions of Express.js.
  6. It helps to simplify the configuration and customization steps for the app.

Express.js Fundamental Concepts

Routing & HTTP Methods

Routing refers to the process of determining a specific behavior of an application. It is used for defining how an application should respond to a client request to a particular route, path, or URI along with a particular HTTP request method. Each route can contain more than one handler function, which is executed when the user browses for a specific route.

Structure of routing in Express.js:

app.METHOD(PATH, HANDLER);
  • app is an instance of Express.js, but you can use any variable.
  • METHOD is the HTTP request method (e.g., get, set, put, delete, etc.).
  • PATH is the route to the server for a specific web page.
  • HANDLER is the callback function that is executed when the matching route is found.

There are 4 main HTTP request methods: GET, POST, PUT, and DELETE.

  • The HTTP GET method aids in requesting the representation of a specific resource for a client. This type of request retrieves data without causing any effect.
  • The HTTP POST method aids in requesting the server to accept the data that is in the request as a new object of the resource as identified by the URI.
  • The HTTP PUT method aids in requesting the server to accept the data that is enclosed within the request as an alteration to the existing object which is identified by the provided URI.
  • The HTTP DELETE method aids in requesting the server to delete a specific resource from the destination.

Middleware

Middleware functions in Express.js have access to request and response objects along with the next function present in the application’s request-response cycle. The functions can perform the following tasks:

  • Execution of any code
  • Modification of the request and the response objects
  • Ending the application’s request-response cycle
  • Calling the next middleware present in the cycle

If the current function doesn’t terminate the request-response cycle, then it must invoke the next() function to pass on control to the next available middleware function or else the request will be left incomplete. Below are commonly used middleware in an Express.js application:

  • Application-level middleware
  • Router-level middleware
  • Error-handling middleware
  • Built-in middleware
  • Third-party middleware

Cookies

In Express.js, cookies are small files of information stored on the user’s computer that hold data specific to a particular user and the websites the user visits. Each time a user loads a website, the browser will automatically send the locally stored data back to the website/server in order to recognize the user. In order to use the cookies, you must npm install the cookie-parser middleware into the pre-existing node_modules folder and then you need to import the cookie-parser into the application.

npm install cookie-parser //in terminal//in applicationvar express = require(‘express’);
var cookieParser = require(‘cookie-parser’);
var app = express();
app.use(cookieParser());

From here, code needs to be written to create and fetch the details of the cookie using a cookie-parser.

REST APIs

REST stands for REpresentational State Transfer, which is an architectural style that is used in web services development. In other words, a REST API is an API (Application Program Interface) that uses the HTTPS requests to GET, PUT, POST, and DELETE the data over the WWW.

Express.js is developed on the middleware module of Node.js called connect, making Express.js the ideal choice for a server since it smooths out the process of creating APIs for the server to communicate as a client with the server application.

Scaffolding

When working with REST APIs, a developer works with various file types (HTML, CSS, JS, etc.). Scaffoldings is the process of creating a proper structure for an application as it helps with managing various file types in proper directories and thus saving the developer from time-consuming manual work. Express.js provides various scaffolding tools, including Yeoman and Express-generator.

Database Connectivity

Of the databases available, the most popular currently are MySQL and MongoDB.

Templating

Templating

A templating engine helps in creating and using the static HTML template with minimal code. When executed, the variables in the template file are replaced by actual values and then the templating engine converts the values into an HTML file. Finally, the HTML file is sent to the client side where more data is injected and the final HTML document is produced. The benefits of this are that it reduces page load time as well as design complexity. Popular templating engines include Pug, Mustache, and Walrus.

Error Handling

Error handling is the mechanism for catching and processing possible errors that might occur during execution of the application. Express takes care of errors with the help of middleware.

Advantages of Express.js

  1. Express.js is easy to learn since many front end users are already familiar with JavaScript. This fact makes it easier for front-end developers to implement backend development using Express.js. Additionally, this framework is great for beginner developers since you only need to know JavaScript and HTML to utilize it. Another
  2. Express.js is a simple framework to customize and use as per the developer’s needs.
  3. Express.js provides a flexible middleware module that is mainly useful for doing extra tasks on response and request.
  4. Express has a highly-supportive, open source developer community due to its popularity.

References

--

--