DEV Community

Chigozie Oduah
Chigozie Oduah

Posted on

Overview of ExpressJS

Introduction

This article covers the basics of the ExpressJS framework, and how to build RESTful APIs using the Express framework in JavaScript. The Express framework makes fast, scalable, and complex APIs very easy to build and maintain.

Prerequisites

To fully understand this article you need the following:

  • Node installed on your system.
  • yarn or npm package manager installed.
  • Working knowledge of JavaScript.
  • An understanding of the REST architecture

What is Express?

Express is a simple and lightweight JavaScript framework for building RESTful APIs. This framework provides a thin layer of abstraction over the Node’s module, and provides utilities for building more complex applications.

REST APIs are APIs that adhere to the REST architectural style requirements. REST APIs provide its interface via the HTTP protocol. These types of APIs are useful in building client-side applications that need to interact with a server.

Features of Express

Express provides a lot of benefits to you as a developer, which are the as follows:

  • Lightweight: The Express library is very small and light, which means that the library won’t take up much space once installed, and the JS runtime won’t take much time loading up the library.
  • Flexible: The Express framework is a light abstraction over the Node JS runtime, which means using Express doesn’t stop you from using native JS functionality in your application. The Flexibility also allows you to install and use all libraries in Node JS.
  • Performance: The Express framework does not reduce the performance of a normal node application. A REST API created using express runs just as fast as an API created using.
  • Robustness: The Express framework presents a lot of usable tools and utilities that can be used to build large scale and complex APIs.

Setup Express

There are two ways to create express applications, which are the following:

  • Installing express into an empty project.
  • Using the express-generator script.

Empty project

To install into an empty project, start by initializing the project:

npm init
Enter fullscreen mode Exit fullscreen mode

Then install the library into the project:

npm install express
Enter fullscreen mode Exit fullscreen mode

After installing the express library, create an index.js file in the project root. By convention, all Node applications always start with an index.js file.

Express Generator

To create a project using the express-generator tool, use the following command:

npx express-generator
Enter fullscreen mode Exit fullscreen mode

The above command creates the application in the current directory.

To create an express project into a new application use the following command:

npx express-generator my-express-app
Enter fullscreen mode Exit fullscreen mode

This command creates a new folder named my-express-app and creates the application in it.

After creating the application skeleton install the project’s dependencies:

npm install
Enter fullscreen mode Exit fullscreen mode

A Simple API using Express

Endpoints are routes of a URL that points to resources served by an API. Endpoints help in organizing REST API interfaces.

To create this application, write the following into the index.js file:

// import the library
const express = require('express');

// create an express application
const app = express();

let recipient = "World";

// register a middleware to all the routes
app.use(express.text());

// create a simple endpoint
app.get("/greet", (req, res) => {

  res.send(`Hello, ${recipient}!`);  // <- the response
});

// create a simple endpoint to change the recipient
app.post("/change-recipient", (req, res) => {

  recipient = req.body;              // <- the body of the request
  res.send(`Hello, ${recipient}`);   // <- the response
});

// launch application on port 8080
app.listen(8080);

console.log("Server running..."); // <- display "Server running…" on the console
Enter fullscreen mode Exit fullscreen mode

To create an endpoint, you need to create the express application first:

// import the library
const express = require('express');

// create an express application
const app = express();
Enter fullscreen mode Exit fullscreen mode

Then you use app.use method to register an express middleware called express.text():

app.use(express.text());
Enter fullscreen mode Exit fullscreen mode

A middleware is a callback function that the app executes before calling your endpoint’s callback function. Middlewares help in processing requests before finally sending a response.

The express.text() middleware function returns an anonymous function, which is the middleware for the application. The app.use() method registers the middleware to all the routes.

Then you create a GET endpoint with the app.get() method:

app.get("/greet", (req, res) => {

  res.send(`Hello, ${recipient}!`);  // <- the response
});
Enter fullscreen mode Exit fullscreen mode

The endpoint in this example is located on the /greet route, and the callback function passed as the second argument is executed whenever you send a request GET to the route

The res.send() method is used to send a response back to a client.

And create a POST endpoint with the app.post() method:

app.post("/change-recipient", (req, res) => {

  recipient = req.body;              // <- the body of the request
  res.send(`Hello, ${recipient}`);   // <- the response


});
Enter fullscreen mode Exit fullscreen mode

The endpoint in this example is located at the /change-recipient route. To get the request body, use the req.body property from the req argument.

This endpoint changes recipient and return a hello message directed to the new recipient.

Before you can run your API you need to specify the port it will run on:

app.listen(8080);
Enter fullscreen mode Exit fullscreen mode

Conclusion

This article covers what express is and its features, how to set up express, and how to create a simple express API. If you want to learn more about the express framework and RESTful APIs be sure to check the following links:

Latest comments (0)