Home  >  Article  >  Web Front-end  >  Instructions for using the url.format method in node.js_node.js

Instructions for using the url.format method in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:27:531986browse

Method description:

Convert a parsed URL object into a formatted URL string.

Grammar:

Copy code The code is as follows:

url.format(urlObj)

Receive parameters:

urlObj represents a URL object, which can contain the following attributes: (see example)

href full path

protocolis Protocol (such as http://)

auth                                                  

hostname Hostname

port Port

host Host (host name port)

pathname ‘/’ can be understood as a directory

query parameter list

search query conditions (that is, the parameter list with “?” added)

hash Hash value

Example:

Copy code The code is as follows:
var url = require('url');

var a = url.format({
protocol: 'http',
auth : null ,
host : 'example.com:8080' ,
port : '8080' ,
hostname : 'example.com' ,
hash: null,
search : '?a=index&t=article&m=default',
query : 'a=index&t=article&m=default',
pathname : '/one',
path : '/one?a=index&t=article&m=default',
href : 'http://example.com:8080/one?a=index&t=article&m=default'
});
console.log(a);

//Output result: http://example.com:8080/one?a=index&t=article&m=default

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn