輕松創(chuàng)建nodejs服務(wù)器(10):處理POST請(qǐng)求
目前為止,我們做的服務(wù)器沒有實(shí)際的用處,接下來我們開始實(shí)現(xiàn)一些實(shí)際有用的功能。
我們要做的是:用戶選擇一個(gè)文件,上傳該文件,然后在瀏覽器中看到上傳的文件。
首先我們需要一個(gè)文本區(qū)(textarea)供用戶輸入內(nèi)容,然后通過POST請(qǐng)求提交給服務(wù)器。
我們?cè)趕tart事件處理器里添加代碼,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ì)象上注冊(cè)監(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;
上述代碼做了三件事情: 首先,我們?cè)O(shè)置了接收數(shù)據(jù)的編碼格式為UTF-8,然后注冊(cè)了“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)文章
nodejs對(duì)文件中的圖片進(jìn)行歸類操作示例
這篇文章主要為大家介紹了nodejs對(duì)文件中的圖片進(jìn)行歸類的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
切換到淘寶最新npm鏡像源的全面指南(支持 Windows、macOS 和多種 Linux
在開發(fā)過程中,npm 是前端開發(fā)者不可或缺的工具,但對(duì)于國內(nèi)的開發(fā)者來說,npm 官方源在下載速度上存在一定的瓶頸,本文將詳細(xì)介紹如何在 Windows、macOS 以及各類 Linux 發(fā)行版上切換到淘寶的 npm 鏡像源,需要的朋友可以參考下2025-03-03
使用puppeteer破解極驗(yàn)的滑動(dòng)驗(yàn)證碼
這篇文章主要介紹了利用puppeteer破解極驗(yàn)的滑動(dòng)驗(yàn)證功能,基本流程代碼實(shí)現(xiàn)給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-02-02
前端必會(huì)的nodejs知識(shí)工具模塊使用示例詳解
這篇文章主要為大家介紹了前端必會(huì)的nodejs知識(shí)工具模塊使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
nodejs文件操作模塊FS(File System)常用函數(shù)簡明總結(jié)
這篇文章主要介紹了nodejs文件操作模塊FS(File System)常用函數(shù)簡明總結(jié),對(duì)FS模塊的大部份異步函數(shù)做了介紹,而且用中文注釋,這下用起來方便了,需要的朋友可以參考下2014-06-06
NodeJS實(shí)現(xiàn)跨域的方法(使用示例)
CORS是一種 W3C 標(biāo)準(zhǔn),它使用額外的 HTTP 頭來告訴瀏覽器讓運(yùn)行在一個(gè) origin (domain) 上的Web應(yīng)用被準(zhǔn)許訪問來自不同源服務(wù)器上的指定的資源,這篇文章主要介紹了NodeJS實(shí)現(xiàn)跨域的方法,需要的朋友可以參考下2024-05-05
nodejs中art-template模板語法的引入及沖突解決方案
本篇文章主要介紹了nodejs中art-template模板語法的引入及沖突解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11

