国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

輕松創(chuàng)建nodejs服務(wù)器(10):處理POST請(qǐng)求

2019-11-20 13:40:02
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

目前為止,我們做的服務(wù)器沒(méi)有實(shí)際的用處,接下來(lái)我們開(kāi)始實(shí)現(xiàn)一些實(shí)際有用的功能。

我們要做的是:用戶選擇一個(gè)文件,上傳該文件,然后在瀏覽器中看到上傳的文件。

首先我們需要一個(gè)文本區(qū)(textarea)供用戶輸入內(nèi)容,然后通過(guò)POST請(qǐng)求提交給服務(wù)器。

我們?cè)趕tart事件處理器里添加代碼,requestHandlers.js修改如下:

復(fù)制代碼 代碼如下:

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;

通過(guò)在瀏覽器中訪問(wèn)http://localhost:8888/start就可以看到效果了。

接下來(lái)我們要實(shí)現(xiàn)當(dāng)用戶提交表單時(shí),觸發(fā)/upload請(qǐng)求處理程序處理POST請(qǐng)求。

為了使整個(gè)過(guò)程非阻塞,Node.js會(huì)將POST數(shù)據(jù)拆分成很多小的數(shù)據(jù)塊,然后通過(guò)觸發(fā)特定的事件,將這些小數(shù)據(jù)塊傳遞給回調(diào)函數(shù)。這里的特定的事件有data事件(表示新的小數(shù)據(jù)塊到達(dá)了)以及end事件(表示所有的數(shù)據(jù)都已經(jīng)接收完畢)。

我們通過(guò)在request對(duì)象上注冊(cè)監(jiān)聽(tīng)器(listener) 來(lái)實(shí)現(xiàn)。這里的 request對(duì)象是每次接收到HTTP請(qǐng)求時(shí)候,都會(huì)把該對(duì)象傳遞給onRequest回調(diào)函數(shù)。

我們把代碼放在服務(wù)器里,server.js修改如下:

復(fù)制代碼 代碼如下:

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)聽(tīng)器,用于收集每次接收到的新數(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ì)用到。

接下來(lái)在/upload頁(yè)面,展示用戶輸入的內(nèi)

我們來(lái)改一下 router.js:

復(fù)制代碼 代碼如下:

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)中:

復(fù)制代碼 代碼如下:

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字段。

我們可以使用此前介紹過(guò)的querystring模塊來(lái)實(shí)現(xiàn):

復(fù)制代碼 代碼如下:

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ù)的全部?jī)?nèi)容。

下一節(jié),我們將實(shí)現(xiàn)圖片上傳的功能。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁德市| 炎陵县| 松溪县| 区。| 平谷区| 五莲县| 吉安市| 甘德县| 安徽省| 香格里拉县| 桑日县| 巴马| 墨脱县| 株洲市| 丹东市| 石嘴山市| 蛟河市| 三河市| 紫金县| 无为县| 贡山| 江孜县| 故城县| 于都县| 莱西市| 义马市| 渝中区| 哈尔滨市| 汕头市| 炎陵县| 衡山县| 郸城县| 随州市| 汶川县| 永和县| 龙陵县| 库车县| 左云县| 临汾市| 交城县| 绥宁县|