轻松创建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 中模拟实现 emmiter 自定义事件
Feb 22 NodeJs
Nodejs学习item【入门手上】
May 05 NodeJs
NodeJS中的MongoDB快速入门详细教程
Nov 11 NodeJs
详解nodejs中exports和module.exports的区别
Feb 17 NodeJs
实例分析nodejs模块xml2js解析xml过程中遇到的坑
Mar 18 NodeJs
nodejs mysql 实现分页的方法
Jun 06 NodeJs
深入学习nodejs中的async模块的使用方法
Jul 12 NodeJs
nodejs操作mongodb的填删改查模块的制作及引入实例
Jan 02 NodeJs
nodejs实现解析xml字符串为对象的方法示例
Mar 14 NodeJs
修改Nodejs内置的npm默认配置路径方法
May 13 NodeJs
CentOS7中源码编译安装NodeJS的完整步骤
Oct 13 NodeJs
如何利用nodejs自动定时发送邮件提醒(超实用)
Dec 01 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生成静态页
2006/11/25 PHP
PHP获得用户使用的代理服务器ip即真实ip
2006/12/31 PHP
php中sprintf与printf函数用法区别解析
2014/02/17 PHP
PHP实现远程下载文件到本地
2015/05/17 PHP
PHP解密Unicode及Escape加密字符串
2015/05/17 PHP
使用图灵api创建微信聊天机器人
2015/07/23 PHP
基于PHP制作验证码
2016/10/12 PHP
php实现自定义中奖项数和概率的抽奖函数示例
2017/05/26 PHP
js常用函数 不错
2006/09/08 Javascript
JavaScript 编程引入命名空间的方法
2007/06/29 Javascript
jquery tools之tabs 选项卡/页签
2009/07/25 Javascript
jquery 表单取值常用代码
2009/12/22 Javascript
JS 按钮点击触发(兼容IE、火狐)
2013/08/07 Javascript
js实现jquery的offset()方法实例
2015/01/10 Javascript
jQuery实现判断滚动条到底部
2015/06/23 Javascript
Ionic实现页面下拉刷新(ion-refresher)功能代码
2016/06/03 Javascript
JS实现一次性弹窗的方法【刷新后不弹出】
2016/12/26 Javascript
jQuery插件JWPlayer视频播放器用法实例分析
2017/01/11 Javascript
ExtJs异步无法向外传值和赋值的完美解决办法
2017/06/14 Javascript
jQuery实现的页面弹幕效果【测试可用】
2018/08/17 jQuery
jQuery简单实现根据日期计算星期几的方法
2019/01/09 jQuery
webpack4手动搭建Vue开发环境实现todoList项目的方法
2019/05/16 Javascript
Python实现类似jQuery使用中的链式调用的示例
2016/06/16 Python
python获取list下标及其值的简单方法
2016/09/12 Python
python中字符串类型json操作的注意事项
2017/05/02 Python
python科学计算之numpy——ufunc函数用法
2019/11/25 Python
keras自定义损失函数并且模型加载的写法介绍
2020/06/15 Python
Python 操作SQLite数据库的示例
2020/10/16 Python
selenium设置浏览器为headless无头模式(Chrome和Firefox)
2021/01/08 Python
python 实现有道翻译功能
2021/02/26 Python
英国最专业的健身器材供应商之一:Best Gym Equipment
2017/12/22 全球购物
毕业生造价工程师求职信
2013/10/17 职场文书
银行优秀员工事迹
2014/02/06 职场文书
会计主管岗位职责
2015/04/02 职场文书
冬季作息时间调整通知
2015/04/24 职场文书
党员承诺书格式范文
2015/04/28 职场文书