手机版
你好,游客 登录 注册
背景:
阅读新闻

Node.js零起点开发教程

处理POST请求

[日期:2012-10-19] 来源:Linux社区  作者:kaosini [字体:大 中 小]

本节我们做一个简单的demo,post数据,并且接受数据

requestHandlers的模块

应用程序需要新的部件,因此加入新的模块 -- 已经无需为此感到新奇了。我们来创建一个叫做requestHandlers的模块,并对于每一个请求处理程序,添加一个占位用函数,随后将这些函数作为模块的方法导出:

requestHandlers的模块

start() : POSt表单HTML

upload : POST数据获取页面

requestHandlers.js

  1. function start(response, postData) { 
  2.   console.log("Request handler 'start' was called."); 
  3.  
  4.   var body = '<html>'+ 
  5.     '<head>'+ 
  6.     '<meta http-equiv="Content-Type" content="text/html; '+ 
  7.     'charset=UTF-8" />'+ 
  8.     '</head>'+ 
  9.     '<body>'+ 
  10.     '<form action="/upload" method="post">'+ 
  11.     '<textarea name="text" rows="20" cols="60"></textarea>'+ 
  12.     '<input type="submit" value="Submit text" />'+ 
  13.     '</form>'+ 
  14.     '</body>'+ 
  15.     '</html>'; 
  16.  
  17.     response.writeHead(200, {"Content-Type": "text/html"}); 
  18.     response.write(body); 
  19.     response.end(); 
  20. } 
  21.  
  22. function upload(response, postData) { 
  23.   console.log("Request handler 'upload' was called."); 
  24.   response.writeHead(200, {"Content-Type": "text/plain"}); 
  25.   response.write("You've sent: " + postData); 
  26.   response.end(); 
  27. } 
  28.  
  29. exports.start = start; 
  30. exports.upload = upload; 

router模块

通过检查给定的路径对应的请求处理程序是否存在,如果存在的话直接调用相应的函数,并返回相应是字符串

router.js

  1. function route(handle, pathname, response, postData) { 
  2.   console.log("About to route a request for " + pathname); 
  3.   if (typeof handle[pathname] === 'function') { 
  4.     handle[pathname](response, postData); 
  5.   } else { 
  6.     console.log("No request handler found for " + pathname); 
  7.     response.writeHead(404, {"Content-Type": "text/plain"}); 
  8.     response.write("404 Not found"); 
  9.     response.end(); 
  10.   } 
  11. } 
  12.  
  13. exports.route = route; 

server模块

处理请求模块

server.js

  1. var http = require("http"); 
  2. var url = require("url"); 
  3.  
  4. function start(route, handle) { 
  5.   function onRequest(request, response) { 
  6.     var postData = ""; 
  7.     var pathname = url.parse(request.url).pathname; 
  8.     console.log("Request for " + pathname + " received."); 
  9.  
  10.     request.setEncoding("utf8"); 
  11.  
  12.     request.addListener("data", function(postDataChunk) { 
  13.       postData += postDataChunk; 
  14.       console.log("Received POST data chunk '"+ 
  15.       postDataChunk + "'."); 
  16.     }); 
  17.  
  18.     request.addListener("end", function() { 
  19.       route(handle, pathname, response, postData); 
  20.     }); 
  21.  
  22.   } 
  23.  
  24.   http.createServer(onRequest).listen(8888); 
  25.   console.log("Server has started."); 
  26. } 
  27.  
  28. exports.start = start; 

index模块

启动模块,主模块

index.js

  1. var server = require("./server"); 
  2. var router = require("./router"); 
  3. var requestHandlers = require("./requestHandlers"); 
  4.  
  5. var handle = {} 
  6. //区分大小写的  
  7. handle["/"] = requestHandlers.start; 
  8. handle["/start"] = requestHandlers.start; 
  9. handle["/upload"] = requestHandlers.upload; 
  10.  
  11. server.start(router.route, handle); 

运行后效果

如果现在启动应用(node index.js,始终记得这个命令行),随后请求一个URL,我请求的是http://localhost:8888/,然后显示的是一个form表单,输入数据后提交表单后就会跳转到http://localhost:8888/upload,你将会看到应用输出相应的信息,这表明我们的HTTP服务器已经在使用路由模块了,并会将请求的路径传递给路由,路由再找到对应的处理函数:

linux
【内容导航】
第1页:NodeJs安装、Hello Word 第2页:require的用法
第3页:Node.Js路由 第4页:路由后添加处理函数
第5页:路由后添加有返回值(return)的处理函数 第6页:处理POST请求
第7页:上传图片并且显示图片
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 小资小资会员 发表于 2015/1/23 11:22:51
文章不错啊,www.hubwiz.com,这个网站也有些不少课程可以学nodejs。