By the end of this chapter, we’ll be able to do the following:

  • Install and use Express to handle HTTP requests.
  • Build a RESTful API with Express.
  • Utilize the Node Package Manager (npm) to install and manage Node.js package dependencies.

For this lesson, we should have basic knowledge of server-side concepts.

Express overview

Node.js is commonly used as a web server to serve up websites, JSON, and more. Express is a popular server-side framework for building web applications on Node.js.

Express provides a lightweight abstraction over the Node.js HTTP modules and convenience methods for creating routing, views, and middleware.

In this chapter, we’ll build a RESTful API that allows API consumers to retrieve, create, update, and delete a set of recipes.

Along the way, we’ll learn about npm and how to use it to manage Node.js packages. We’ll learn how to configure Express to return HTML templates and JSON data to the user. We’ll also learn about RESTful design principles.

The Node Package Manager and Node community

The Node Package Manager (npm) consists of three distinct components:

  • The npm is the world’s largest JavaScript software registry. Open-source developers from across the globe use npm to share and borrow packages, and many organizations use npm to manage private development as well. The extensive public database of packages means there’s a package for almost everything! We can download and adapt these packages for our applications or incorporate them as they are. However, since anybody can publish a package to the npm registry, npm packages might not all be of the same quality. Often, there are dozens or even hundreds of packages with similar names and purposes, so it’s essential to select the most appropriate package for our project.

  • The npm also provides a user interface for managing our public and private packages and other aspects of our npm experience, like creating organizations to coordinate package maintenance and restricting code to specific developers.

  • The npm also refers to the command-line interface (CLI) tool for downloading and managing Node.js packages. It comes bundled with our Node.js installation and is how most developers interact with the npm registry. While npm is the standard package manager for Node.js, there are other alternatives, such as Yarn. In this chapter, we’ll only use npm.

What criteria would we apply to select packages for our project?

When we use the search bar on the npm website website to find packages, it returns a list of packages that can be ranked by four criteria:

  • Popularity: How popular is the package? If a package has thousands or even millions of downloads, that’s probably a strong indicator that others have found the package to be useful. Check out npm trends, where we can compare the popularity of different comparable packages.

  • Quality: What steps have the package creator or maintainer taken to ensure high quality? The presence of a README file, package stability, tests, and up-to-date dependencies are characteristics of a high-quality package.

  • Maintenance: How well maintained is the package? Is the package frequently updated to work well with other dependencies or new versions of the npm CLI?

  • Optimality: How optimal is the package? Optimal combines the three criteria mentioned above, popularity, quality, and maintenance, into one meaningful score.

The npm search bar is powered by npms and the npm’s analyzer.

Get hands-on with 1400+ tech skills courses.