1、自己写
const http = require('http');
http.createServer(function(req,res){
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;
}
console.log(url,get);
res.write('aaa');
res.end();
}).listen(8080);
2、引入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(url,get);
res.write('aaa');
res.end();
}).listen(8080);
3、引入url模块
const http = require('http');
const urlli = require('url');
http.createServer(function(req,res){
var obj = urlli.parse(req.url,true); //true解析,如果没有true,默认不解析
var url = obj.pathname;
var get = obj.query;
console.log(url,get);
res.write('aaa');
res.end();
}).listen(8080);
提交表单的HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="http://localhost:8080/aaa" method="get">
用户:<input type="text" name="user" value=''><br>
密码:<input type="password" name="pass" value=''><br>
<input type="submit" value="提交">
</form>
</body>
</html>