Node學習筆記之第五課接收前臺發送的數據POST

部分的筆記在上一章裏

直接上代碼

 <!-- post請求的 -->
    <form action="http://localhost:8888/login" method="POST">
        用戶名:
        <input type="text" name="name" id="">
        密碼:
        <input type="password" name="pass" id="">
        <input type="submit" name="" value="提交">
    </form>

server.js

const http = require('http');
const querystring = require('querystring')

var server = http.createServer(function (req, res) {
    var post={}
    var postStr='';
    //這裏表示有一段數據發送的時候這裏就執行一次
    req.on('data',function(data){
        postStr+=data;
    });

    //這裏代表post請求數據發送完畢的時候執行  只執行一次
    req.on('end',function(){
        // console.log(postStr);   //name=111&pass=111
        post=querystring.parse(postStr);
        console.log(post)   //{ name: '111', pass: '111' }
    });

    res.end();
})

server.listen(8888);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章