//**方法一:手动切 const http=require("http");//引入http模块 http.createServer(function(req,res){//通过http模块对象创建服务器 //req获取前台发的数据 var GET ={}; if (req.url.indexOf('?')!=-1) { var arr = req.url.split('?'); var url=arr[0];//地址 var arr2 = arr[1].split('&'); for (var i = 0; i < arr2.length; i++) { var arr3 = arr2[i].split('='); GET[arr3[0]]=arr3[1]; } }else{ var url=req.url; // GET={} } console.log(url,GET); res.write("aaa");//响应写在页面上的内容 res.end();//结束响应 }).listen(8080);//要个端口并监听 //**方法二:使用querystring模块 const http = require('http'); const querystring=require('querystring'); http.createServer(function(req,res){ var GET={}; if (req.url.indexOf('?')!=-1) { var arr=req.url.split('?'); var url = arr[0]; GET=querystring.parse(arr[1]);// }else{ var url=req.url; } console.log(GET); res.write('aaaa'); res.end(); }).listen(8080); //**方法三:使用url模块 const http = require('http'); const urlLib = require('url'); var GET={}; http.createServer(function(req,res){ var obj=urlLib.parse(req.url,true); var url=obj.pathname; var GET=obj.query; console.log(url,GET); res.write('aaaaa'); res.end(); }).listen(8080);