nodejs参数的接收(post和get)

get提交和接收:

routerhtml.js

var http=require('http');
var url=require('url');
var router=require('../module/tuwen.js');
http.createServer(function(request,response){

    if(request.url !== '/favicon.ico'){//清除第二次访问
        console.log('访问');
        var pathname=url.parse(request.url).pathname;
        pathname=pathname.replace(/\//,'');//替换前面/
        console.log(pathname)
        router[pathname](request,response);
        // response.end("");//不写会没有协议尾部,但是写了会访问俩次
    }
}).listen(8007);
console.log('Server running at http://127.0.0.1:8007/')

 tuwen.js

var optfile = require('../module/optfile.js');
var imgFile=require('../module/image.js');
var url=require('url');

module.exports={
    buny:function(req,res){
        //get方式接收参数
        var rdata = url.parse(req.url,true).query;
        console.log(rdata);
        if(rdata['login'] !=undefined){
            console.log("login:" + rdata['login']);
            console.log("pass:" + rdata['pass']);
        }
        
        //闭包,回调这个函数,客户端打印程序
        function recall(data){
            res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
            res.write(data);
            res.end("");
        }
        optfile.readfile('../view/buny.html',recall)
    },
    showImg:function (req,res){
        res.writeHead(200,{'Content-Type':'image/jpeg'});
        imgFile.readImg('../imgs/vue.png',res);
    }
}

post提交参数:这里做修改

<form action="./buny" method="post">
var optfile = require('../module/optfile.js');
var imgFile=require('../module/image.js');
var url=require('url');
var querystring = require('querystring');//post导入
module.exports={
    buny:function(req,res){
        
        //post方式接收参数
        var post='';//定义一个post变量,用于暂存请求体信息
        req.on('data',function(chunk){//通过req的data时间监听函数,每当接收到请求体数据,就会存储到post变量中
            post += chunk;
        });
        req.on('end',function(){//在end事件触发后,通过querystring.parse将post解析为真正的post格式,然后向客户端返回
            post = querystring.parse(post);
            console.log('login:'+post['login']+'\n');
            console.log('pass:'+post['pass']+'\n');
            optfile.readfile('../view/buny.html',recall)
        });
        //闭包,回调这个函数,客户端打印程序
        function recall(data){
            res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
            res.write(data);
            res.end("");
        }
        
    },
    showImg:function (req,res){
        res.writeHead(200,{'Content-Type':'image/jpeg'});
        imgFile.readImg('../imgs/vue.png',res);
    }
}

buny.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
喜欢你
<img src="/showImg"/>
<form action="./buny" method="get">
    <table>
        <tr>
            <td>用户名:</td>
            <td><input type="text" name="login"></td>
        </tr>
        <tr>
            <td>密码:</td>
            <td><input type="password" name="pass"></td>
        </tr>

        <tr>
            <td align="center"><input type="submit" value="登录"></td>
        </tr>
    </table>

</form>
</body>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值