標籤:node.js 表單上傳
今天按照《node.js入門》這本書學習了node的一些基礎知識,包括伺服器的建立,路由功能的實現,表單上傳和資料處理,感覺開始明白了node.js的一些基本原理。這本書說的很詳細也很基礎,很適合初學者學習。node.js入門
眾所周知,node跟php語言不一樣,node不需要依賴於apache等伺服器,因為node本身就能夠構建伺服器!所以,再用node開發網站之前我們首先得學會如何搭建伺服器。關於node建立伺服器在我之前的部落格已經有介紹,這裡不再贅述。
完成一個表單上傳與資料處理的demo
1.明確功能,先確定需要建立哪些檔案(由於只是demo,我們不需要寫html檔案,涉及前端的簡單處理就好。)
伺服器檔案server.js;程式入口檔案index.js;路由檔案router.js;請求處理檔案 handleRequest.js
首先我們來寫入口檔案:
<span style="font-size:18px;">var server= require("./server.js");//引入server.jsvar router = require("./router.js");var handle = require("./requestHandlers");var handles ={};//關聯陣列用來傳遞函數。handles["/"] = handle.start;//這裡主要是為了後面的路由功能,使用者訪問localhost:3000/start就會直接轉到start方法handles["/start"] = handle.start;handles["/upload"] = handle.upload;server.start(router.route,handles);</span>
在index檔案裡面,我們初始化一個關聯陣列,用來存放需要用到的函數。並且開啟伺服器。
server檔案:
<span style="font-size:18px;">var http = require("http"); //node.js 內建的http模組var url= require("url");function start(route,handles){//建立一個HTTP伺服器;http.createServer( function (req, res) { //匿名函數//大部分伺服器會在使用者嘗試訪問localhost:8888時候訪問localhost:8888/favicon.ico,這裡去除這一訪問結果if(req.url == '/favicon.ico'){return;}var postData = "";var pathname = url.parse(req.url).pathname;//解析出url裡面的路徑名req.setEncoding("utf8");req.addListener("data",function(postDataChunk){//通過回呼函數的方式擷取post資料postData += postDataChunk ;});req.addListener("end",function(){route(handles,pathname,res,postData);});console.log("Requset"+pathname+"arrived!");}).listen(3000);//偵聽3000連接埠號碼console.log("HTTP server has started!");} //將server中的代碼寫在一個start函數裡面。並且將該函數匯出,一次完成代碼的模組化,在其餘的檔案中只需要以 var http = require("http")的形式進行調用; exports.start = start;</span>
首先,我們設定了接收資料的編碼格式為 UTF-8,然後註冊了“data”事件的監聽器,用於收集每次接收到的新資料區塊,並將其賦值給 postData 變數,最後,我們將請求路由的調用移到 end 事件處理常式中,以確保它只會當所有資料接收完畢後才觸發,並且只觸發一次。我們同時還把 POST 資料傳遞給請求路由,因為這些資料,請求處理常式會用到。為了使整個過程非阻塞,Node.js會將POST資料拆分成很多小的資料區塊,然後通過觸發特定的事件,將這些小資料區塊傳遞給回呼函數。這裡的特定的事件有 data 事件(表示新的小資料區塊到達了)以及 end 事件(表示所有的資料都已經接收完畢)。
router.js:
<span style="font-size:18px;">function route(handles,pathname,res,postData){console.log("About to route a request to"+pathname);if(typeof handles[pathname] === 'function'){//通過對路徑名稱的判斷,調用相應的函數進行處理,這是路由的基本思想。handles[pathname](res,postData);}else{console.log("No request handler found for " + pathname); }}exports.route=route;</span>
route函數傳遞三個參數,關聯陣列handles(儲存requestHandlers裡面的函數),pathname是訪問路徑,res是回應對象,postData是post接收的數組。
requestHandles.js
var querystring = require("querystring");function start(res){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>'; console.log("Request Handlers for 'start' was called");res.writeHead(200,{"Content-Type":"text/html"});res.write(body);res.end();}function upload(res,postData){res.writeHead(200,{"Content-Type":"text/plain"});var temp_data = querystring.parse(postData).text;res.write(temp_data);res.end();console.log("Reques Handlers for 'upload ' was called");}exports.start = start;exports.upload = upload;這個頁面裡面,start函數首先向使用者發送了一個網頁,包含輸入框和按鈕,使用者輸入文字點擊提交,資料被傳入upload檔案並顯示。upload檔案用於顯示。
node.js學習(2)--路由功能以及表單上傳