Building an HTTP server to accept GET requests
HTTP stands for HyperText Transfer Protocol and is an application layer protocol that underpins the World Wide Web (WWW). HTTP enables communication between servers and browsers. In this recipe, we will use the Node.js core APIs to build an HTTP server that will accept GET requests only.
Important note
When building large complex applications, it is typical to implement these using a higher-level framework rather than interacting with core Node.js APIs. However, having an understanding of the underlying APIs is important, and in some cases only interacting with the underlying Node.js APIs will provide you with the fine-grained control required in certain circumstances.
Getting ready
Start by creating a directory for this recipe, and a file named server.js
that will contain our HTTP server:
$ mkdir http-server $ cd http-server $ touch server.js
How to do it…
For this recipe, we will be using the core Node.js...