輕松創(chuàng)建nodejs服務(wù)器(10):處理POST請(qǐng)求
目前為止,我們做的服務(wù)器沒有實(shí)際的用處,接下來我們開始實(shí)現(xiàn)一些實(shí)際有用的功能。
我們要做的是:用戶選擇一個(gè)文件,上傳該文件,然后在瀏覽器中看到上傳的文件。
首先我們需要一個(gè)文本區(qū)(textarea)供用戶輸入內(nèi)容,然后通過POST請(qǐng)求提交給服務(wù)器。
我們在start事件處理器里添加代碼,requestHandlers.js修改如下:
function start(response) {
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) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello Upload");
response.end();
}
exports.start = start;
exports.upload = upload;
通過在瀏覽器中訪問http://localhost:8888/start就可以看到效果了。
接下來我們要實(shí)現(xiàn)當(dāng)用戶提交表單時(shí),觸發(fā)/upload請(qǐng)求處理程序處理POST請(qǐng)求。
為了使整個(gè)過程非阻塞,Node.js會(huì)將POST數(shù)據(jù)拆分成很多小的數(shù)據(jù)塊,然后通過觸發(fā)特定的事件,將這些小數(shù)據(jù)塊傳遞給回調(diào)函數(shù)。這里的特定的事件有data事件(表示新的小數(shù)據(jù)塊到達(dá)了)以及end事件(表示所有的數(shù)據(jù)都已經(jīng)接收完畢)。
我們通過在request對(duì)象上注冊監(jiān)聽器(listener) 來實(shí)現(xiàn)。這里的 request對(duì)象是每次接收到HTTP請(qǐng)求時(shí)候,都會(huì)把該對(duì)象傳遞給onRequest回調(diào)函數(shù)。
我們把代碼放在服務(wù)器里,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;
上述代碼做了三件事情: 首先,我們設(shè)置了接收數(shù)據(jù)的編碼格式為UTF-8,然后注冊了“data”事件的監(jiān)聽器,用于收集每次接收到的新數(shù)據(jù)塊,并將其賦值給postData 變量,最后,我們將請(qǐng)求路由的調(diào)用移到end事件處理程序中,以確保它只會(huì)當(dāng)所有數(shù)據(jù)接收完畢后才觸發(fā),并且只觸發(fā)一次。我們同時(shí)還把POST數(shù)據(jù)傳遞給請(qǐng)求路由,因?yàn)檫@些數(shù)據(jù),請(qǐng)求處理程序會(huì)用到。
接下來在/upload頁面,展示用戶輸入的內(nèi)
我們來改一下 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;
然后,在requestHandlers.js中,我們將數(shù)據(jù)包含在對(duì)upload請(qǐng)求的響應(yīng)中:
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;
我們最后要做的是: 當(dāng)前我們是把請(qǐng)求的整個(gè)消息體傳遞給了請(qǐng)求路由和請(qǐng)求處理程序。我們應(yīng)該只把POST數(shù)據(jù)中,我們感興趣的部分傳遞給請(qǐng)求路由和請(qǐng)求處理程序。在我們這個(gè)例子中,我們感興趣的其實(shí)只是text字段。
我們可以使用此前介紹過的querystring模塊來實(shí)現(xiàn):
var querystring = require("querystring");
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 the text: "+ querystring.parse(postData).text);
response.end();
}
exports.start = start;
exports.upload = upload;
好了,以上就是關(guān)于處理POST數(shù)據(jù)的全部內(nèi)容。
下一節(jié),我們將實(shí)現(xiàn)圖片上傳的功能。
- nodejs處理http請(qǐng)求實(shí)例詳解之get和post
- Node.js中的HTTP?Server對(duì)象與GET、POST請(qǐng)求
- nodejs 使用http進(jìn)行post或get請(qǐng)求的實(shí)例(攜帶cookie)
- nodejs使用http模塊發(fā)送get與post請(qǐng)求的方法示例
- nodejs實(shí)現(xiàn)HTTPS發(fā)起POST請(qǐng)求
- node.js+postman實(shí)現(xiàn)模擬HTTP服務(wù)器與客戶端交互
- 從零開始學(xué)習(xí)Node.js系列教程一:http get和post用法分析
- nodejs之get/post請(qǐng)求的幾種方式小結(jié)
- Node.js如何響應(yīng)Ajax的POST請(qǐng)求并且保存為JSON文件詳解
- NodeJS收發(fā)GET和POST請(qǐng)求的示例代碼
- Node發(fā)出HTTP POST請(qǐng)求的方法實(shí)例小結(jié)
相關(guān)文章
node.js基于socket.io快速實(shí)現(xiàn)一個(gè)實(shí)時(shí)通訊應(yīng)用
這篇文章主要介紹了node.js基于socket.io快速實(shí)現(xiàn)一個(gè)實(shí)時(shí)通訊應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04基于promise.js實(shí)現(xiàn)nodejs的promises庫
promise是JavaScript實(shí)現(xiàn)優(yōu)雅編程的一個(gè)非常不錯(cuò)的輕量級(jí)框架。該框架可以讓你從雜亂的多重異步回調(diào)代碼中解脫出來,并把精力集中到你的業(yè)務(wù)邏輯上。2014-07-07使用Node.js實(shí)現(xiàn)Clean?Architecture方法示例詳解
這篇文章主要為大家介紹了使用Node.js實(shí)現(xiàn)Clean?Architecture方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02nodejs中用npm初始化來創(chuàng)建package.json的實(shí)例講解
今天小編就為大家分享一篇nodejs中用npm初始化來創(chuàng)建package.json的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10NodeJS和BootStrap分頁效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了NodeJS和BootStrap分頁效果的實(shí)現(xiàn)代碼的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11Node.js創(chuàng)建一個(gè)Express服務(wù)的方法詳解
這篇文章主要介紹了Node.js創(chuàng)建一個(gè)Express服務(wù)的方法,結(jié)合實(shí)例形式分析了node.js創(chuàng)建Express服務(wù)的具體步驟、實(shí)現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下2020-01-01node.js多個(gè)異步過程中判斷執(zhí)行是否完成的解決方案
這篇文章主要給大家介紹了關(guān)于node.js多個(gè)異步過程中判斷執(zhí)行是否完成的幾種解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-12-12