Node.js之接收前台数据实例

<form class="form-horizontal" method="post" action="http:127.0.0.1:3000/post">
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">姓名</label>
            <div class="col-sm-4">
                <input type="text" name="username" class="form-control" id="inputEmail3">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">性别</label>
            <div class="col-sm-4">
                <label class="radio-inline">
                    <input type="radio" name="sex" value="男" checked></label>
                <label class="radio-inline">
                    <input type="radio" name="sex" value="女"></label>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">爱好</label>
            <div class="col-sm-4">
                <label class="checkbox-inline">
                    <input type="checkbox" name="hobby" checked value="把妹"> 把妹
                </label>
                <label class="checkbox-inline">
                    <input type="checkbox" name="hobby" value="睡觉"> 睡觉
                </label>
                <label class="checkbox-inline">
                    <input type="checkbox" name="hobby" value="dota"> Dota
                </label>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-default">提交</button>
            </div>
        </div>
    </form>

 

/**
 * 原生node.js接收前台post请求数据
 * 1、引入http,querystring(字符串解析)模块
 * 2、创建http服务器,监听3000端口
 * 3、判断路由是否为post和请求类型是否为post,如果是则进入请求接收处理
 * 4、声明变量alldata 存储接收到的数据
 * 5、绑定http请求的data事件,接收存储数据字符串
 * 6、绑定http请求的end事件,处理接收到的数据字符串,转换为对象
 * 7、输出
 */

//引入模块
var http = require('http');
var querystring = require('querystring');

//创建服务器
var server = http.createServer(function (req, res) {
    if (req.url === '/post' && req.method.toLowerCase() === 'post') {
        var alldata = '';
        req.on('data', function (chunk) {
            alldata += chunk;
        });

        req.on('end', function () {
            res.end('success');
            //将字符串转换位一个对象
            console.log(alldata); //username=%E6%9D%8E%E5%9B%9B&sex=%E5%A5%B3&hobby=%E7%9D%A1%E8%A7%89&hobby=%E6%89%93%E8%B1%86%E8%B1%86
            var dataString = alldata.toString();
            //将接收到的字符串转换位为json对象
            var dataObj = querystring.parse(dataString);
            //输出数据
            console.log(dataObj);  //{ username: '王五', sex: '男', hobby: [ '把妹', '睡觉', 'Dota' ] }
            console.log(dataObj.username); //王五
            console.log(dataObj.sex); //
            console.log(dataObj.hobby); //[ '把妹', '睡觉', 'Dota' ]
        });
    };
});
//设置监听端口
server.listen(3000, "127.0.0.1", function () {
    console.log("server is started listen port 3000");
});

 

posted @ 2017-08-18 10:52  JokerJason  阅读(1595)  评论(0)    收藏  举报

It's not who you are underneath, it's what you do that defines you

Brick walls are there for a reason :they let us prove how badly we want things