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服務器已經在使用路由模塊了,並會將請求的路徑傳遞給路由,路由再找到對應的處理函數:


本篇文章來源於 Linux公社網站(www.linuxidc.com)  原文鏈接:http://www.linuxidc.com/Linux/2012-10/72627p6.htm

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章