跳到主要内容
CNode

文档里的几个参数的解释,不明白,求解释。能举几个例子么?

社区
Gggaaooppeenngg发布于 13 年前最后回复 13 年前
649230

url.parse(urlStr, [parseQueryString], [slashesDenoteHost])# Take a URL string, and return an object.

Pass true as the second argument to also parse the query string using the querystring module. Defaults to false.

Pass true as the third argument to treat //foo/bar as { host: 'foo', pathname: '/bar' } rather than { pathname: '//foo/bar' }. Defaults to false.

查看回复

回复 (6)

J
jiyinyiyong#613 年前
引用 jiyinyiyong跑了个例子:

@ggaaooppeenngg 这里只是第二个参数, 选项为 true 时多解析出了 query: { a: '3', b: '4' }, 第三个参数文档上比较明确, 是 //foo/bar 解析为 { host: 'foo', pathname: '/bar' } 或者 { pathname: '//foo/bar' } 的差别

G
ggaaooppeenngg#413 年前
引用 jiyinyiyong跑了个例子:

哦,我知道了,第一个true是把query变成JSON格式,第二个是把url解析成JSON格式,是么?

J
jiyinyiyong#313 年前

跑了个例子:

➤➤ coffee
coffee> url = require 'url'
{ parse: [Function: urlParse],
  resolve: [Function: urlResolve],
  resolveObject: [Function: urlResolveObject],
  format: [Function: urlFormat],
  Url: [Function: Url] }
coffee> url.parse '/a?a=3&b=4', no
{ protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: null,
  search: '?a=3&b=4',
  query: 'a=3&b=4',
  pathname: '/a',
  path: '/a?a=3&b=4',
  href: '/a?a=3&b=4' }
coffee> url.parse '/a?a=3&b=4', yes
{ protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: null,
  search: '?a=3&b=4',
  query: { a: '3', b: '4' },
  pathname: '/a',
  path: '/a?a=3&b=4',
  href: '/a?a=3&b=4' }
coffee>
C
chloe#213 年前

同学以后最到这样的问题的时候自己先动手 2,第二个参数如果是true,表示会用 querystring 这个模块来解析查询字符串 3.第三个参数表是是否//后面的作为host,并且以host为key加入到返回值中

R
redky#113 年前

Pass true as the second argument to also parse the query string using the querystring module. Defaults to false.

第二个参数是 true 时, 就使用 querystring 模块 parse query( ?a=b -> {a:b}) 部分.

参与回复

登录后即可参与回复。登录