Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node基础篇之url和querystring #34

Open
kekobin opened this issue Sep 19, 2019 · 0 comments
Open

node基础篇之url和querystring #34

kekobin opened this issue Sep 19, 2019 · 0 comments

Comments

@kekobin
Copy link
Owner

kekobin commented Sep 19, 2019

URL

node官网url模块部分提供了一张图,清晰的展示了url各部分的组成:

image

最实用方法

  • parse(urlString):将url字符串,解析成object,便于开发者进行操作。

url解析:url.parse()

完整语法如下:

url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
  • parseQueryString:(默认为false)如为false,则urlObject.query为未解析的字符串,比如name%3D%25E6%259F%25AF%25E6%2596%258C,且对应的值不会decode;如果parseQueryString为true,则urlObject.query为object,比如{ name: '阿斌' },且值会被decode;
  • slashesDenoteHos:(默认为false)如果为true,那么类似//foo/bar里的foo就会被认为是hostname;如果为false,则foo被认为是pathname的一部分。

image

querystring

主要用来做url查询参数的解析,功能跟上面的url.parse第二个参数为true时获取到的query一样。

主要是如下两个方法:

  • .parse():对url查询参数(字符串)进行解析,生成易于分析的json格式。
  • .stringify():跟**.parse()**相反,用于拼接查询查询。

parse()

querystring.parse(str[, sep[, eq[, options]]])

image

stringify()

querystring.stringify(obj[, sep[, eq[, options]]])

image

@kekobin kekobin changed the title node基础篇之url模块 node基础篇之url和querystring Sep 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant