Function resolve

  • The url.resolve() method resolves a target URL relative to a base URL in a manner similar to that of a web browser resolving an anchor tag.

    const url = require('url');
    url.resolve('/one/two/three', 'four'); // '/one/two/four'
    url.resolve('http://example.com/', '/one'); // 'http://example.com/one'
    url.resolve('http://example.com/one', '/two'); // 'http://example.com/two'

    To achieve the same result using the WHATWG URL API:

    function resolve(from, to) {
    const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
    if (resolvedUrl.protocol === 'resolve:') {
    // `from` is a relative URL.
    const { pathname, search, hash } = resolvedUrl;
    return pathname + search + hash;
    }
    return resolvedUrl.toString();
    }

    resolve('/one/two/three', 'four'); // '/one/two/four'
    resolve('http://example.com/', '/one'); // 'http://example.com/one'
    resolve('http://example.com/one', '/two'); // 'http://example.com/two'

    Since

    v0.1.25

    Deprecated

    Legacy: Use the WHATWG URL API instead.

    Parameters

    • from: string

      The base URL to use if to is a relative URL.

    • to: string

      The target URL to resolve.

    Returns string

  • The url.resolve() method resolves a target URL relative to a base URL in a manner similar to that of a web browser resolving an anchor tag.

    const url = require('url');
    url.resolve('/one/two/three', 'four'); // '/one/two/four'
    url.resolve('http://example.com/', '/one'); // 'http://example.com/one'
    url.resolve('http://example.com/one', '/two'); // 'http://example.com/two'

    To achieve the same result using the WHATWG URL API:

    function resolve(from, to) {
    const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
    if (resolvedUrl.protocol === 'resolve:') {
    // `from` is a relative URL.
    const { pathname, search, hash } = resolvedUrl;
    return pathname + search + hash;
    }
    return resolvedUrl.toString();
    }

    resolve('/one/two/three', 'four'); // '/one/two/four'
    resolve('http://example.com/', '/one'); // 'http://example.com/one'
    resolve('http://example.com/one', '/two'); // 'http://example.com/two'

    Since

    v0.1.25

    Deprecated

    Legacy: Use the WHATWG URL API instead.

    Parameters

    • from: string

      The base URL to use if to is a relative URL.

    • to: string

      The target URL to resolve.

    Returns string

Generated using TypeDoc