URL Parse As Address

1.0.0 · abandoned · verified Wed Apr 22

url-parse-as-address is a JavaScript utility for parsing URL strings, specifically designed to assume the `http` protocol if no scheme or leading `//` is explicitly provided. This behavior mimics how web browsers handle incomplete URL inputs, making it suitable for scenarios where user-entered addresses might lack full protocol specification (e.g., 'foo.com' instead of 'http://foo.com'). Published as version 1.0.0 over a decade ago, this package primarily functions as a CommonJS module. It provides a parsed object structure similar to Node.js's legacy `url.parse()` method, including properties like `protocol`, `host`, `pathname`, and `query`. Due to its age and lack of updates, newer projects are generally advised to use the WHATWG `URL` API (built into Node.js and modern browsers) or more actively maintained alternatives that offer better security, features, and ESM support, especially given its limited ecosystem adoption (17 dependents).

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic URL parsing with `url-parse-as-address`, showing how it defaults to `http:` protocol and how to optionally parse query strings into an object.

const parse = require('url-parse-as-address');
const assert = require('assert');

const urlString = 'foo.com:1234/x?y=z#a=b';

// Basic parsing, query string not parsed into an object
assert.deepEqual(parse(urlString),
  { protocol: 'http:',
    slashes: true,
    auth: null,
    host: 'foo.com:1234',
    port: '1234',
    hostname: 'foo.com',
    hash: '#a=b',
    search: '?y=z',
    query: 'y=z',
    pathname: '/x',
    path: '/x?y=z',
    href: 'http://foo.com:1234/x?y=z#a=b' });

// Parsing with query string as an object (second argument `true`)
assert.deepEqual(parse(urlString, true),
  { protocol: 'http:',
    slashes: true,
    auth: null,
    host: 'foo.com:1234',
    port: '1234',
    hostname: 'foo.com',
    hash: '#a=b',
    search: '?y=z',
    query: { y: 'z' },
    pathname: '/x',
    path: '/x?y=z',
    href: 'http://foo.com:1234/x?y=z#a=b' });

console.log('All assertions passed!');

view raw JSON →