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]+
);
            console.log(pass:+post[pass]+
);
            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>
经验分享 程序员 微信小程序 职场和发展