如何在node环境实现“get数据解析”代码实例

(编辑:jimmy 日期: 2024/12/27 浏览:2)

1、自己写

const http = require('http');
 
http.createServer(function(req,res){
  var get = {};
  if(req.url.indexOf('"htmlcode">
const http = require('http');
const querystring = require('querystring');
http.createServer(function(req,res){
  var get = {};
  if(req.url.indexOf('"htmlcode">
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>

个人练习如下:

const http = require("http");
const queryString = require("querystring");
const urlNode = require('url');

http.createServer((req,res)=>{
  console.log(req.url);  // /aaa"([^", "gi");
    // var regSingle = new RegExp("([^", "i");
    // var arrAll = query.match(regAll);   //["user=123", "pass=456"]
    // arrAll.map(function(v,k,arr){
    //   var resultArr = v.match(regSingle);
    //   get[resultArr[1]] = resultArr[2];
    // })
    //方法三:引入querystring模块
    // const arr = req.url.split('"htmlcode">
app.get('/article',(req,res)=>{
  console.log(req.query)
  
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。