Module 1 - Node.js & Express

Objectives

Traditionally, developers only used the JavaScript language in web browsers. But, in 2009, Node.js was unveiled, and with it, the developer tool kit expanded greatly. Node.js gave developers the chance to use JavaScript to write software that, up to that point, could only be written using C, C++, Java, Python, Ruby, C#, and the like.

We will use Node to write server code. Specifically, web services that communicate with clients using the JavaScript Object Notation (JSON) format for data interchange.

Advantages of Node.js

  • Node.js uses the same programming language (JavaScript) and paradigm for both client and server. Using the same language, we minimize context switching and share code between the client and the server.
  • JavaScript is single-threaded, which removes the complexity involved in handling multiple threads.
  • JavaScript is asynchronous, which allows us to take full advantage of the processor it's running on. Taking full advantage of the processor is crucial because the node process will be running on a single CPU.
  • Using JavaScript gives us access to the npm repository. This repository is the largest ecosystem of valuable libraries (most free to use) in npm modules.

Disadvantages of Node.js

  • By strictly using JavaScript on the server, we cannot use the right tool (a particular language) for the job.
  • Because JavaScript is single-threaded, we can't take advantage of servers with multiple cores/processors.
  • Because JavaScript is asynchronous, it is harder to learn for developers that have only worked with languages that default to synchronous operations that block the execution thread.
  • In the npm repository, there are often too many packages that do the same thing. This excess of packages makes it harder to choose one and, in some cases, may introduce vulnerabilities into our code.

Core Concepts of Node.js Development

  • Event-Driven Architecture - Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
  • NPM (Node Package Manager) - NPM is the world's largest software registry with over 1 million packages, allowing developers to easily share and reuse code.
  • Single-threaded Event Loop - Node.js processes incoming requests in a single thread, using non-blocking I/O calls to support thousands of concurrent connections without incurring the cost of thread context switching.
  • CommonJS Module System - Node.js implements the CommonJS module specification, making it easy to organize and reuse code across files and projects.

Writing Web Servers with Node.js

To write a simple web server with Node.js:

  1. Use Node's HTTP module to abstract away complex network-related operations.
  2. Write the single request handler function to handle all requests to the server.

The request handler is a function that takes the request coming from the client and produces the response. The function takes two arguments: 1) an object representing the request and 2) an object representing the response.

This process works, but the resulting code is verbose, even for the simplest of servers. Also, note that when using only Node.js to build a server, we use a single request handler function for all requests.

Challenge

Write a paragraph about what Node.js is and explain at least 3 of its core features.

Consider addressing the following points in your response:

  • What problem does Node.js solve in web development?
  • How does Node.js handle asynchronous operations?
  • What are the advantages of using JavaScript on both client and server?
  • How does the npm ecosystem contribute to Node.js development?

Guided Project

Project Resources

Important Notes

The versions of project dependencies used in the recording are slightly different from the ones used in the starter and solution repositories, but this should not affect the relevant code of the Guided Project.

The versions used in the repositories are more recent, and thus more similar to the versions you will install if you create a project from scratch.

Assignment

Project Resources