轻松创建nodejs服务器(9):实现非阻塞操作


Posted in NodeJs onDecember 18, 2014

我们要将response对象(从服务器的回调函数onRequest()获取)通过请求路由传递给请求处理程序。随后,处理程序就可以采用该对象上的函数来对请求作出响应。

我们先对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对象作为第三个参数传递给route()函数,并且,我们将onRequest()处理程序中所有有关response的函数调都移除,因为我们希望这部分工作让route()函数来完成。

接下来修改 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;

同样的模式:相对此前从请求处理程序中获取返回值,这次取而代之的是直接传递response对象。 如果没有对应的请求处理器处理,我们就直接返回“404”错误。

接下来修改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;

我们的处理程序函数需要接收response参数,为了对请求作出直接的响应。 start处理程序在exec()的匿名回调函数中做请求响应的操作,而upload处理程序仍然是简单的回复“Hello World”,只是这次是使用response对象而已。

如果想要证明/start处理程序中耗时的操作不会阻塞对/upload请求作出立即响应的话,可以将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;

这样一来,当请求http://localhost:8888/start的时候,会花10秒钟的时间才载入,而当请求http://localhost:8888/upload的时候,会立即响应,纵然这个时候/start响应还在处理中。

NodeJs 相关文章推荐
在NodeJS中启用ECMAScript 6小结(windos以及Linux)
Jul 15 NodeJs
轻松创建nodejs服务器(3):代码模块化
Dec 18 NodeJs
Windows系统下使用Sublime搭建nodejs环境
Apr 13 NodeJs
nodejs和C语言插入mysql数据库乱码问题的解决方法
Apr 14 NodeJs
用nodejs实现json和jsonp服务的方法
Aug 25 NodeJs
nodejs使用express获取get和post传值及session验证的方法
Nov 09 NodeJs
详解nodeJs文件系统(fs)与流(stream)
Jan 24 NodeJs
Nodejs下使用gm圆形裁剪并合成图片的示例
Feb 22 NodeJs
nodejs express配置自签名https服务器的方法
May 22 NodeJs
nodejs 如何手动实现服务器
Aug 20 NodeJs
Sublime Text3 配置 NodeJs 环境的方法
May 20 NodeJs
nodejs+koa2 实现模仿springMVC框架
Oct 21 NodeJs
轻松创建nodejs服务器(6):作出响应
Dec 18 #NodeJs
轻松创建nodejs服务器(5):事件处理程序
Dec 18 #NodeJs
轻松创建nodejs服务器(4):路由
Dec 18 #NodeJs
轻松创建nodejs服务器(3):代码模块化
Dec 18 #NodeJs
轻松创建nodejs服务器(2):nodejs服务器的构成分析
Dec 18 #NodeJs
轻松创建nodejs服务器(1):一个简单nodejs服务器例子
Dec 18 #NodeJs
Nodejs实现多人同时在线移动鼠标的小游戏分享
Dec 06 #NodeJs
You might like
解析如何在PHP下载文件名中解决乱码的问题
2013/06/20 PHP
smarty内置函数capture用法分析
2015/01/22 PHP
PHP计算日期相差天数实例分析
2016/02/23 PHP
Laravel 集成微信用户登录和绑定的实现
2019/12/27 PHP
JavaScript 输入框内容格式验证代码
2010/02/11 Javascript
javascript中onmouse事件在div中失效问题的解决方法
2012/01/09 Javascript
jquery offset函数应用实例
2012/11/14 Javascript
jquery中获取元素里某一特定子元素的代码
2014/12/02 Javascript
Javascript连接Access数据库完整实例
2015/08/03 Javascript
jQuery form插件之ajaxForm()和ajaxSubmit()的可选参数项对象
2016/01/23 Javascript
JS正则替换掉小括号及内容的方法
2016/11/29 Javascript
JS产生随机数的用法小结
2016/12/10 Javascript
基于JavaScript实现微信抢红包功能
2017/07/20 Javascript
Angular 2.0+ 的数据绑定的实现示例
2017/08/09 Javascript
puppeteer实现html截图的示例代码
2019/01/10 Javascript
koa2服务端使用jwt进行鉴权及路由权限分发的流程分析
2019/07/22 Javascript
ES6 Iterator遍历器原理,应用场景及相关常用知识拓展详解
2020/02/15 Javascript
js代码实现轮播图
2020/05/04 Javascript
微信小程序调用wx.getImageInfo遇到的坑解决
2020/05/31 Javascript
vue-cli3项目配置eslint代码规范的完整步骤
2020/09/10 Javascript
python字符串加密解密的三种方法分享(base64 win32com)
2014/01/19 Python
Python使用redis pool的一种单例实现方式
2016/04/16 Python
简单谈谈Python中的几种常见的数据类型
2017/02/10 Python
python使用tensorflow保存、加载和使用模型的方法
2018/01/31 Python
PyQt打开保存对话框的方法和使用详解
2019/02/27 Python
Tensorflow实现酸奶销量预测分析
2019/07/19 Python
Python合并2个字典成1个新字典的方法(9种)
2019/12/19 Python
6PM官网:折扣鞋、服装及配饰
2018/08/03 全球购物
小班秋游活动方案
2014/02/22 职场文书
园林系毕业生求职信
2014/06/23 职场文书
学校清明节活动总结
2014/07/04 职场文书
上课玩手机的检讨书
2014/10/01 职场文书
2014年党风建设工作总结
2014/11/19 职场文书
python 三边测量定位的实现代码
2021/04/22 Python
详解Laravel制作API接口
2021/05/31 PHP
如何解决goland,idea全局搜索快捷键失效问题
2022/04/03 Golang