Chapter 4. Building Node web applications


This chapter covers

  • Handling HTTP requests with Node’s API
  • Building a RESTful web service
  • Serving static files
  • Accepting user input from forms
  • Securing your application with HTTPS

In this chapter, you’ll become familiar with the tools Node provides for creating HTTP servers, and you’ll get acquainted with the fs (filesystem) module, which is necessary for serving static files. You’ll also learn how to handle other common web application needs, such as creating low-level RESTful web services, accepting user input through HTML forms, monitoring file upload progress, and securing a web application with Node’s Secure Sockets Layer (SSL).

At Node’s core is a powerful streaming HTTP parser consisting of roughly 1,500 lines of optimized C, written by the author of Node, Ryan Dahl. This parser, in combination with the low-level TCP API that Node exposes to JavaScript, provides you with a very low-level, but very flexible, HTTP server.

Like most modules in Node’s core, the http module favors simplicity. High-level “sugar” APIs are left for third-party frameworks, such as Connect or Express, that greatly simplify the web application building process. Figure 4.1 illustrates the anatomy of a Node web application, showing that the low-level APIs remain at the core, and that abstractions and implementations are built on top of those building blocks.

Figure 4.1. Overview of the layers that make up a Node web application

4.1. HTTP server fundamentals

4.2. Building a RESTful web service

4.3. Serving static files

4.4. Accepting user input from forms

4.5. Securing your application with HTTPS

4.6. Summary