NODE URL模块-GET参数处理
拿到 GET 请求传递过来的参数
使用URL模块, 语法示例如下
url.format(urlObject) url.parse(urlString[, parseQueryString[, slashesDenoteHost]]) url.format(urlObject): 将路径转换为对象 url.parse(urlString[, parseQueryString[, slashesDenoteHost]]): 将对象转换为路径 let url = require("url"); let http = require("http"); let str = "http://root:123456@www.it6666.top:80/index.html?name=BNTang&age=68#banner"; let obj = url.parse(str, true); console.log(obj); console.log(obj.query.name); console.log(obj.query.age); let url = require("url"); let http = require("http"); // 1.创建一个服务器实例对象 let server = http.createServer(); server.on("request", function (req, res) { console.log(req.url); let obj = url.parse(req.url, true); res.end(obj.query.name + "----" + obj.query.age); }); // 2.指定监听的端口 server.listen(3000);