你的分享就是我们的动力 ---﹥

nodejs处理POST请求

时间:2013-03-16 22:03来源:www.chengxuyuans.com 点击:
本节我们做一个简单的nodejs处理POST请求 demo,post数据,并且接受数据
 
requestHandlers的模块
 
应用程序需要新的部件,因此加入新的模块 -- 已经无需为此感到新奇了。我们来创建一个叫做requestHandlers的模块,并对于每一个请求处理程序,添加一个占位用函数,随后将这些函数作为模块的方法导出:
 
requestHandlers的模块
 
start() : POSt表单HTML
 
upload : POST数据获取页面
 
requestHandlers.js
 
function start(response, postData) { 
  console.log("Request handler 'start' was called."); 
 
  var body = '<html>'+ 
    '<head>'+ 
    '<meta http-equiv="Content-Type" content="text/html; '+ 
    'charset=UTF-8" />'+ 
    '</head>'+ 
    '<body>'+ 
    '<form action="/upload" method="post">'+ 
    '<textarea name="text" rows="20" cols="60"></textarea>'+ 
    '<input type="submit" value="Submit text" />'+ 
    '</form>'+ 
    '</body>'+ 
    '</html>'; 
 
    response.writeHead(200, {"Content-Type": "text/html"}); 
    response.write(body); 
    response.end(); 
 
function upload(response, postData) { 
  console.log("Request handler 'upload' was called."); 
  response.writeHead(200, {"Content-Type": "text/plain"}); 
  response.write("You've sent: " + postData); 
  response.end(); 
 
exports.start = start; 
exports.upload = upload; 
router模块
 
通过检查给定的路径对应的请求处理程序是否存在,如果存在的话直接调用相应的函数,并返回相应是字符串
 
router.js
 
function route(handle, pathname, response, postData) { 
  console.log("About to route a request for " + pathname); 
  if (typeof handle[pathname] === 'function') { 
    handle[pathname](response, postData); 
  } else { 
    console.log("No request handler found for " + pathname); 
    response.writeHead(404, {"Content-Type": "text/plain"}); 
    response.write("404 Not found"); 
    response.end(); 
  } 
 
exports.route = route; 
server模块
 
处理请求模块
 
server.js
 
var http = require("http"); 
var url = require("url"); 
 
function start(route, handle) { 
  function onRequest(request, response) { 
    var postData = ""; 
    var pathname = url.parse(request.url).pathname; 
    console.log("Request for " + pathname + " received."); 
 
    request.setEncoding("utf8"); 
 
    request.addListener("data", function(postDataChunk) { 
      postData += postDataChunk; 
      console.log("Received POST data chunk '"+ 
      postDataChunk + "'."); 
    }); 
 
    request.addListener("end", function() { 
      route(handle, pathname, response, postData); 
    }); 
 
  } 
 
  http.createServer(onRequest).listen(8888); 
  console.log("Server has started."); 
 
exports.start = start; 
index模块
 
启动模块,主模块
 
index.js
 
var server = require("./server"); 
var router = require("./router"); 
var requestHandlers = require("./requestHandlers"); 
 
var handle = {} 
//区分大小写的  
handle["/"] = requestHandlers.start; 
handle["/start"] = requestHandlers.start; 
handle["/upload"] = requestHandlers.upload; 
 
server.start(router.route, handle); 
运行后效果
 
如果现在启动应用(node index.js,始终记得这个命令行),随后请求一个URL,我请求的是http://localhost:8888/,然后显示的是一个form表单,输入数据后提交表单后就会跳转到http://localhost:8888/upload,你将会看到应用输出相应的信息,这表明我们的HTTP服务器已经在使用路由模块了,并会将请求的路径传递给路由,路由再找到对应的处理函数:

转载注明地址:http://www.chengxuyuans.com/nodejs/2013/0316/53349.html