Working with URLs and HTTP in Node.js
Network programming is a fundamental aspect of most programming languages, and Node.js provides robust capabilities for handling HTTP operations. While the built-in HTTP module operates at a low level without features like routing or session management, understanding its core functionality is essential for mastering Node.js web development.
Understanding Uniform Resource Locators (URLs)
URLs follow a standardized structure that can be broken down into components:
http://user:pass@host.com:80/resource/path/?query=string#hash
This breaks down as: protocol://authentication@hostname:port/path/query#fragment
The URL module exposes several key methods and properties for working with these components:
exports.parse = urlParse;
exports.resolve = urlResolve;
exports.resolveObject = urlResolveObject;
exports.format = urlFormat;
exports.Url = Url;
function Url() {
this.protocol = null;
this.slashes = null;
this.auth = null;
this.host = null;
this.port = null;
this.hostname = null;
this.hash = null;
this.search = null;
this.query = null;
this.pathname = null;
this.path = null;
this.href = null;
}
Parsing URL Strings into Objects
The parse method converts URL strings into structured objects for easier manipulation:
const url = require('url');
const address = 'http://user:pass@host.com:80/resource/path?query=string#hash';
// parse(urlString, [parseQueryString], [slashesDenoteHost])
const parsedUrl = url.parse(address, true, false);
console.log(parsedUrl);
This produces output showing all URL components:
Url {
protocol: 'http:',
slashes: true,
auth: 'user:pass',
host: 'host.com:80',
port: '80',
hostname: 'host.com',
hash: '#hash',
search: '?query=string',
query: { query: 'string' },
pathname: '/resource/path',
path: '/resource/path?query=string',
href: 'http://user:pass@host.com:80/resource/path?query=string#hash'
}
Resolving Relative URLs
When working with relative paths, the resolve method helps convert them to absolute URLs:
const url = require('url');
const baseUrl = 'http://user:pass@host.com:80/resource/path?query=string#hash';
const relativePath = '/different/route?param=value';
console.log(url.resolve(baseUrl, relativePath));
// Output: http://user:pass@host.com:80/different/route?param=value
Handling Query Parameters and Form Data
Web applications frequently need to process query strings and form data. The querystring module provides tools for parsing and serializing these parameters:
const querystring = require('querystring');
// Parse query string to object
// parse(str, [separator], [assignment], [options])
const params = querystring.parse("username=john&colors=red&colors=blue");
console.log(params);
// Output: { username: 'john', colors: ['red', 'blue'] }
// Convert object back to query string
// stringify(obj, [separator], [assignment], [options])
console.log(querystring.stringify(params, "&", "="));
// Output: username=john&colors=red&colors=blue
The parsing function accepts optional parameters to customize delimiter behavior and set limits on key count for security purposes.