Node URL类

URL类使用实例获取参数

url.searchParams.get(key)

var http = require('http');
http.createServer(function (req,res) {
  if(req.url != '/favicon.ico'){
    const url = new URL(`${req.url}`,'http://localhost:3000/')
    console.log(url.searchParams);
    let name = url.searchParams.get('name')
    let age = url.searchParams.get('age')
    console.log(name,age);
  }
  res.writeHead(200, {'Content-type':"text/html;charset='utf-8'"})
  res.write("<head> <meta charset='UTF-8'> </head>")
  res.write("你好,node.js")
  res.write("<button>确实</button>")
  res.end("11111")
}).listen(3000)
  • input <string> 要解析的绝对或相对的输入网址。 如果 input 是相对的,则需要 base。 如果 input 是绝对的,则忽略 base。
  • base <string> | <URL> 如果 input 不是绝对的,则为要解析的基本网址。

通过相对于 base 解析 input 来创建新的 URL 对象。 如果 base 作为字符串传入,则其将被解析为等效于 new URL(base)。

Const myURL = newURL(‘/foo’, ‘https://example.org/‘);

以下是URL实例

  href: ‘http://localhost:3000/?name=%22ccy%22&age=%2223%22‘,

  origin: ‘http://localhost:3000‘,

  protocol: ‘http:’,

  username: ”,

  password: ”,

  host: ‘localhost:3000’,

  hostname: ‘localhost’,

  port: ‘3000’,

  pathname: ‘/’,

  search: ‘?name=%22ccy%22&age=%2223%22’,

  searchParams: URLSearchParams { ‘name’ => ‘”ccy”‘, ‘age’ => ‘”23″‘ },

  hash: ”

来自 <http://nodejs.cn/api/url.html#url_new_url_input_base>

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇