HTTP
HTTP
Node.js
• Implementing HTTP Services in Node.js:
Processing URLs,
Understanding Request, Response, and Server Objects,
Implementing HTTP Clients and Servers in Node.js,
Implementing HTTPS Servers and Clients
Processing URLs
• The Uniform Resource Locator (URL) acts as an address label for the
HTTP server to handle requests from the client.
• It provides all the information needed to get the request to the
correct server on a specific port and access the proper data.
• The URL can be broken down into several different components,
each providing a basic piece of information for the webserver on how
to route and handle the HTTP request from the client.
URL Components
• Figure illustrates the basic structure of a URL and the components
that may be included. Not all these components are included in every
HTTP request.
Creation of URL object
• HTTP requests from the client include the URL string with the information
• To create a URL object from the URL string, pass the URL string as the first
• parameter to the following method:
• url.parse(urlStr, [parseQueryString], [slashesDenoteHost])
• The url.parse() method takes the URL string as the first parameter. The
• parseQueryString parameter is a Boolean that when true also parses the
• query string portion of the URL into an object literal. The default is false. The
• slashesDenoteHost is also a Boolean that when true parses a URL with the
• format of //host/path to {host: 'host', pathname: '/path'}
• instead of {pathname: '//host/path'}. The default is false.
• You can also convert a URL object into a string form using the
following url.parse() method. Table lists the attributes of the URL
objects created by url.parse():
• url.format(urlObj)
Resolving the URL Components
• Another useful feature of the url module is the ability to resolve URL
components in the same manner as a browser would. This allows you to
manipulate the URL strings on the server side to make adjustments in the
URL.
• For example, you might want to change the URL location before processing
the request because a resource has moved or changed parameters.
• To resolve a URL to a new location use the following syntax:
• url.resolve(from, to)
• The from parameter specifies the original base URL string. The to parameter
specifies the new location where you want the URL to resolve.
http module
• One of the most important aspects of Node.js is the ability to quickly
implement HTTP and HTTPS servers and services. Node.js provides the
http and https modules
• the http module for is implementing backend web services for your
applications to use. That is where the http module becomes an
invaluable tool in your arsenal.
• You can create basic HTTP servers that provide an interface for
communications behind your firewall and then basic HTTP clients that
interact with those services.
• the http module is pretty low level. It doesn’t provide calls to handle
routing, cookies, caching, and so on. When you get to the
• Express chapters later in this book, you will see the advantages it
provides.
The Built-in HTTP Module
• Node.js has a built-in module called HTTP, which allows Node.js to
transfer data over the Hyper Text Transfer Protocol (HTTP).
Node JS Built-in Objects http
module
1. ClientRequest,
2. ServerResponse,
3. IncomingMessage,
4. and Server objects.
• The most important events, properties, and methods that each
provides also are covered.
The http.ClientRequest Object
• The ClientRequest object is created internally when you call
http.request() when building the HTTP client.
• This object represents the request while it is in progress to the server.
You use the ClientRequest object to initiate, monitor, and handle the
response from the server.
• The ClientRequest implements a Writable stream, so it provides all the
functionality of a Writable stream object. For example, you can use the
write() method to write to it as well as pipe a Readable stream into it.
• To implement a ClientRequest object, you use a call to http.request()
• using the following syntax: http.request(options, callback)
The http.ServerResponse Object
• The ServerResponse object is created by the HTTP server internally
when a request event is received. It is passed to the request event
handler as the second argument. You use the ServerRequest object to
formulate and send a response to the client.
• The ServerResponse implements a Writable stream, so it provides all
the functionality of a Writable stream object. For example, you can
use the write() method to write to it as well as pipe a Readable
stream into it to write data back to the client.
The http.Server Object
• The Node.js HTTP Server object provides the fundamental framework
to implement HTTP servers. It provides an underlying socket that
listens on a port and handles receiving requests and then sends
responses out to client connections.
• Creating an HTTPS client and server is almost exactly like the process of creating an
HTTP client discussed earlier in this chapter.
• The most important options you really need to worry about are key, cert, and agent.
• The key option specifies the private key used for SSL. The cert value specifies the
x509 public key to use. The global agent does not support options needed by HTTPS,
so you need to disable the agent by setting the agent to null, as shown here:
var options = {
key: fs.readFileSync('test/keys/client.pem'),
cert: fs.readFileSync('test/keys/client.crt),
agent: false
};
Creating an HTTPS Server