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: ”