輕松創(chuàng)建nodejs服務(wù)器(9):實(shí)現(xiàn)非阻塞操作
我們要將response對(duì)象(從服務(wù)器的回調(diào)函數(shù)onRequest()獲取)通過請(qǐng)求路由傳遞給請(qǐng)求處理程序。隨后,處理程序就可以采用該對(duì)象上的函數(shù)來對(duì)請(qǐng)求作出響應(yīng)。
我們先對(duì)server.js做出修改:
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對(duì)象作為第三個(gè)參數(shù)傳遞給route()函數(shù),并且,我們將onRequest()處理程序中所有有關(guān)response的函數(shù)調(diào)都移除,因?yàn)槲覀兿M@部分工作讓route()函數(shù)來完成。
接下來修改 router.js:
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;
同樣的模式:相對(duì)此前從請(qǐng)求處理程序中獲取返回值,這次取而代之的是直接傳遞response對(duì)象。 如果沒有對(duì)應(yīng)的請(qǐng)求處理器處理,我們就直接返回“404”錯(cuò)誤。
接下來修改requestHandler.js:
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ù),為了對(duì)請(qǐng)求作出直接的響應(yīng)。 start處理程序在exec()的匿名回調(diào)函數(shù)中做請(qǐng)求響應(yīng)的操作,而upload處理程序仍然是簡(jiǎn)單的回復(fù)“Hello World”,只是這次是使用response對(duì)象而已。
如果想要證明/start處理程序中耗時(shí)的操作不會(huì)阻塞對(duì)/upload請(qǐng)求作出立即響應(yīng)的話,可以將requestHandlers.js修改為如下形式:
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)請(qǐng)求http://localhost:8888/start的時(shí)候,會(huì)花10秒鐘的時(shí)間才載入,而當(dāng)請(qǐng)求http://localhost:8888/upload的時(shí)候,會(huì)立即響應(yīng),縱然這個(gè)時(shí)候/start響應(yīng)還在處理中。
相關(guān)文章
nodejs處理http請(qǐng)求實(shí)例詳解之get和post
最近一段時(shí)間在學(xué)習(xí)前端向服務(wù)器發(fā)送數(shù)據(jù)和請(qǐng)求數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于nodejs處理http請(qǐng)求實(shí)例詳解之get和post的相關(guān)資料,需要的朋友可以參考下2023-01-01
使用Nodejs開發(fā)微信公眾號(hào)后臺(tái)服務(wù)實(shí)例
這篇文章主要介紹了使用Nodejs開發(fā)微信公眾號(hào)后臺(tái)服務(wù)實(shí)例,在這個(gè)實(shí)例中,主要使用到了express, wechat, mongodb, monk等模塊,需要的朋友可以參考下2014-09-09
Node實(shí)現(xiàn)搜索框進(jìn)行模糊查詢
這篇文章主要為大家詳細(xì)介紹了Node實(shí)現(xiàn)搜索框進(jìn)行模糊查詢,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
nodeJS?express路由學(xué)習(xí)req.body與req.query方法實(shí)例詳解
這篇文章主要為大家介紹了nodeJS?express路由學(xué)習(xí)req.body與req.query方法實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
node.js?readline和line-reader逐行讀取文件方法
Readline是Node的原生模塊。它是專門為從任何可讀流逐行讀取內(nèi)容而開發(fā)的。它可用于從命令行讀取數(shù)據(jù),line-reader模塊是Node.js中逐行讀取文件的開源模塊。它不是本地模塊,所以你需要使用npm(節(jié)點(diǎn)包管理器)安裝它2022-10-10
NodeJS服務(wù)器實(shí)現(xiàn)gzip壓縮的示例代碼
這篇文章主要介紹了NodeJS服務(wù)器實(shí)現(xiàn)gzip壓縮的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10

