解析get方式发送的请求
来源:     阅读:282
易浩激活码
发布于 2022-03-15 14:13
查看主页
http fs 接受前台传过来的数据请求(解析get方式发送的请求)

要求:get post ajax form 后端:转换成对象
form表单发送数据 转换对象格式
uname=Tom&upwd=123456 {uname:Tom,upwd:123456}

在day02文件夹中创立from.html文件与server1.js文件

from.html文件

<!DOCTYPE html><html>    <head>        <meta charset="utf-8">        <title></title>    </head>    <body>        <form action="http://localhost:8080" method="GET">            <p>客户名:<input type="text" name="uname"></p>            <p>密码:<input type="text" name="upwd"></p>            <p><input type="submit" name="" id="" value="提交" /></p>        </form>    </body></html>
方法一:

server1.js

const http=require('http');var server=http.createServer(function(req,res){    GET=[]    var arr=req.url.split('?');    //console.log(arr);//['/','uname=Tom&upwd=123456']    var arr1=arr[1].split('&');    //console.log(arr1);//['uname=Tom','upwd=123456']        //遍历数组    for(var i=0;i<arr1.length;i++){        var arr2 = arr1[i].split('=');        //console.log(arr2);//["uname",'Tom'],['upwd','123456']        GET[arr2[0]]=arr2[1];        console.log(GET);//[uname:'Tom',upwd:'123456']    }})server.listen(8080);
Image 5.png
方法二:

创立server2.js

//方法二:const http=require('http');const querystring=require('querystring');var server=http.createServer(function(req,res){    var GET=[]    var arr=req.url.split('?');    GET=querystring.parse(arr[1]);    console.log(GET);})server.listen(8080);
Image 6.png

方法三

url模块
const http= require('http');consr urls = require('url');var server=http.createServer(function(req,res){  var urlLis=urls.parse('http://www.baidu.com/index?uname=Tom&upwd=123456',true);  console.log(urlLis);  console.log(urlLis.query);//{uname:'Tom',upwd:'123456'}});server.listen(8080);
免责声明:本文为用户发表,不代表网站立场,仅供参考,不构成引导等用途。 系统环境 软件环境
相关推荐
Hoverfly - 微服务虚拟化示例
无镜--kafka之元数据缓存
70%的web前台学习者最后都放弃的起因
10.14系统无法打开“xx”,由于无法确认开发者的身份_解决方法
Linux Command——top
首页
搜索
订单
购物车
我的