nodejs学习-post请求处理

http_post.js代码:

var process = require('process');

var listen_port = 3000;

//服务器接收请求,将相应数据写回
//客户端发出请求,接收响应

http.createServer(
    function(req, res){
        var buf = new Buffer(0);
        //接收请求数据
        req.on('data', function(data){
                //data的类型是Buffer
                buf = Buffer.concat([buf, data]);
        });

        //接收请求数据结束
        req.on('end', function(){
                console.log(buf.toString('Utf-8'));
                res.write(buf.toString('utf-8'));
                res.end();
                //退出进程
                process.exit();
        });

    }
).listen(listen_port);
console.log('Server start on port 3000');


post.html代码:

<html>
<head>
<meta charset="UTF-8" content="text/html" http-equiv="Content-Type">
</head>
<body>
<form method="POST" action="http://127.0.0.1:3000">
    <table>
        <thead>
            <tr>
                <p>这是一个POST提交到node.js服务器的例子程序</p>
            </tr>
        </thead>
        <tbody>
            <tr>
                <input type="text" name="title">
            </tr>
            <tr>
                <textarea name="content" ></textarea>
            </tr>
            <tr>
                <input type="submit">
            </tr>
        </tbody>
    </table>
</form>
</body>
</html>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值