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

首頁 > 語言 > JavaScript > 正文

輕松創(chuàng)建nodejs服務(wù)器(9):實(shí)現(xiàn)非阻塞操作

2024-05-06 16:12:22
字體:
供稿:網(wǎng)友
這篇文章主要介紹了輕松創(chuàng)建nodejs服務(wù)器(9):實(shí)現(xiàn)非阻塞操作,本系列文章會教你一步一步創(chuàng)建一個(gè)完整的服務(wù)器,要的朋友可以參考下
 
 

我們要將response對象(從服務(wù)器的回調(diào)函數(shù)onRequest()獲取)通過請求路由傳遞給請求處理程序。隨后,處理程序就可以采用該對象上的函數(shù)來對請求作出響應(yīng)。

我們先對server.js做出修改:

 

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

var http = require("http");
var url = require("url");
function start(route, handle) {
  function onRequest(request, response) {
 var pathname = url.parse(request.url).pathname;
 console.log("Request for " + pathname + " received."); 
 route(handle, pathname, response); 
  }
  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}
exports.start = start;

 

我們將response對象作為第三個(gè)參數(shù)傳遞給route()函數(shù),并且,我們將onRequest()處理程序中所有有關(guān)response的函數(shù)調(diào)都移除,因?yàn)槲覀兿M@部分工作讓route()函數(shù)來完成。

接下來修改 router.js:

 

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

function route(handle, pathname, response) {
  console.log("About to route a request for " + pathname);
  if (typeof handle[pathname] === 'function') {
 handle[pathname](response);
  } 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;

 

同樣的模式:相對此前從請求處理程序中獲取返回值,這次取而代之的是直接傳遞response對象。 如果沒有對應(yīng)的請求處理器處理,我們就直接返回“404”錯(cuò)誤。

接下來修改requestHandler.js:

 

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

var exec = require("child_process").exec;
function start(response) {
  console.log("Request handler 'start' was called.");
  exec("ls -lah", function (error, stdout, stderr) {
 response.writeHead(200, {"Content-Type": "text/plain"});
 response.write(stdout);
 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;

 

我們的處理程序函數(shù)需要接收response參數(shù),為了對請求作出直接的響應(yīng)。 start處理程序在exec()的匿名回調(diào)函數(shù)中做請求響應(yīng)的操作,而upload處理程序仍然是簡單的回復(fù)“Hello World”,只是這次是使用response對象而已。

如果想要證明/start處理程序中耗時(shí)的操作不會阻塞對/upload請求作出立即響應(yīng)的話,可以將requestHandlers.js修改為如下形式:

 

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

var exec = require("child_process").exec;
function start(response) {
  console.log("Request handler 'start' was called.");
  exec("find /",
      { timeout: 10000, maxBuffer: 20000*1024 },
      function (error, stdout, stderr) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write(stdout);
  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;

 

這樣一來,當(dāng)請求http://localhost:8888/start的時(shí)候,會花10秒鐘的時(shí)間才載入,而當(dāng)請求http://localhost:8888/upload的時(shí)候,會立即響應(yīng),縱然這個(gè)時(shí)候/start響應(yīng)還在處理中。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 苏尼特右旗| 吉木乃县| 郁南县| 罗山县| 柯坪县| 南宁市| 临夏县| 嘉定区| 德阳市| 山丹县| 仁布县| 克山县| 马鞍山市| 若尔盖县| 西安市| 泾阳县| 兴城市| 洮南市| 吉林市| 涞源县| 灌南县| 岢岚县| 基隆市| 金沙县| 衡东县| 车险| 田阳县| 通河县| 太白县| 天祝| 武夷山市| 英吉沙县| 洪泽县| 延津县| 平阳县| 探索| 泸西县| 龙海市| 蒲城县| 苗栗市| 康保县|