node.js 接收post请求
来源:互联网 发布:手机apk软件开发 编辑:程序博客网 时间:2024/05/17 08:32
部分内容参照 http://www.holdyourdream.com/blog/?p=25 和 http://cnodejs.org/blog/?p=2207
使用data和end事件来获取post数据,代码如下:
var http = require('http');var server = http.createServer();var querystring = require('querystring');var firstPage = function(res){res.writeHead(200, {'Content-Type': 'text/html'});var html = '<html><body>'+'<form action="/login" method="post">'+'name:<input type="text" name="name"> </br>'+'password:<input type="password" name="pwd"></br>'+'<input type="submit" value="login">'+'</form>'+'</body></html>';res.end(html);}var login = function(req, res) {var info ='';req.addListener('data', function(chunk){info += chunk; }).addListener('end', function(){info = querystring.parse(info);if(info.name == 'a' && info.pwd =='1'){res.end('login success ' + info.name);}else{res.end('login failed ' + info.name);} })}var requestFunction = function (req, res){if(req.url == '/'){return firstPage(res);}if(req.url == '/login'){if (req.method != 'POST'){return;}return login(req, res)}}server.on('request',requestFunction);server.listen(1337, "127.0.0.1");console.log('Server running at http://127.0.0.1:1337/');
以上代码实现了一个登录验证用户,将以上代码保存到 example4.js
cmd键入 node example4.js
浏览器地址栏键入 http://127.0.0.1:1337/
如果输入name:a password:1 点击login
如果输入name:a password:2 点击login
该例子 from使用的是默认的application/x-www-form-urlencoded,如果同时要上传文件的话使用multipart/form-data,如何解析请参照http://cnodejs.org/blog/?p=2207
- node.js 接收post请求
- Node.js中http 接收get请求、接收post请求、发送get请求、发送post请求
- node.js 接收get请求
- node接收get与post请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- node.js处理get和post请求
- Node.js GET与POST请求
- Node.js GET/POST请求简单例子
- node.js中get和post请求
- node.js发送http(get,post)请求
- Node.js基础get post请求
- node 接收处理post
- sharepoint 2007 将%complete作为条件判断时,工作流无法正常工作
- android资料共享
- Qt的元对象(Meta-Object)系统简介
- 一致性哈希相关资料
- 二叉排序树的创建和查找
- node.js 接收post请求
- sql--case when then 示例
- MVC初探
- 横竖屏切换解决方案
- 串口控制台只能发不能收
- linux 第二天
- 公有继承方式public
- android Intent 启动拨号 实现打电话
- apacheBench