Resolving the URL Components

A 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 URL strings on the server side to make adjustments in a URL. For example, you might want to change the URL location before processing a 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 to which you want the URL to resolve. The following code shows an example of resolving a URL to a new location.

var url = require('url'),
var originalUrl = 'http://user:[email protected]:80/resource/path?query=string#hash';
var newResource = '/another/path?querynew';
console.log(url.resolve(originalUrl, newResource));

The output of this code snippet is shown below:

http://user:[email protected]:80/another/path?querynew

Notice that only the resource path and beyond are altered in the resolved URL location.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset