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.