Node.js用于获取执行上下文的编程模式 [英] Node.js Programming Pattern for getting Execution Context

查看:159
本文介绍了Node.js用于获取执行上下文的编程模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在node.js中编写一个网络应用程序。现在,服务器上的每个处理总是处于会话的上下文中,该会话在请求命中服务器的第一阶段被检索或创建。之后,执行流程通过其中的多个模块和回调。我正在努力创建一个编程模式,以便在代码中的任何时候,会话对象可用,而没有程序员要求它在每个函数调用中作为参数传递。



如果所有的代码都在一个单一的文件中,我可能有一个关闭,但是如果有其他文件中的其他模块的函数调用,我如何编程,以便会话对象在被调用的函数中可用而不通过它作为一个论据。我觉得两个文件中的两个函数之间应该有一些联系,但是如何安排这是我被卡住的地方。



一般来说,我想说一下总是执行上下文,其可以是会话或网络请求,其处理分散在多个文件上,并且执行上下文对象将在所有点都可用。实际上可以有多种用例,例如每个网络请求有一个Log对象或每个会话一个Log对象。而这项工作所需要的管道应该安装在侧面,没有应用程序员烦扰它。他只是知道那个执行环境在所有地方都可以使用。



我认为这是所有人面临的常见问题,所以请给我一些想法。 b
$ b

以下是问题

  MainServer.js 


app = require('express')。createServer();
app_module1 = require('AppModule1');
var session = get_session();
app.get('/ my / page',app_module1.func1);

AppModule1.js

app_module2 = require('AppModule2');
exports.func1 = function(req,res){

//我想知道这个代码运行的会话上下文是

app_module2.func2( REQ,RES);

}

AppModule2.js

exports.func2 = function(req,res){

// I想知道这个代码运行的会话上下文

}


解决方案

您可以使用域 - 一个新的节点0.8功能实现这一点。这个想法是在自己的域中运行每个请求,为每个请求数据提供一个空间。您可以访问当前请求的域,而无需通过process.domain传递它。



以下是使其与express配合使用的示例:
如何使用Node.js 0.8。 x domain with express?



请注意,一般来说,域一定是实验性的,process.domain特别是没有文档的(尽管在0.8中显然不会消失是一些关于使其永久性的讨论)。我建议按照他们的建议,并在process.domain.data中添加特定于应用程序的属性。



https://github.com/joyent/node/issues/3733



https://groups.google.com/d/msg/nodejs-dev/gBpJeQr0fWM/-y7fzzRMYBcJ


I am writing a web app in node.js. Now every processing on the server is always in the context of a session which is either retrieved or created at the very first stage when the request hits the server. After this the execution flows through multiple modules and callbacks within them. What I am struggling with is in creating a programming pattern so that at any point in the code the session object is available without the programmer requiring it to pass it as an argument in each function call.

If all of the code was in one single file I could have had a closure but if there are function calls to other modules in other files how do I program so that the session object is available in the called function without passing it as an argument. I feel there should be some link between the two functions in the two files but how to arrange that is where I am getting stuck.

In general I would like to say there is always a execution context which could be a session or a network request whose processing is spread across multiple files and the execution context object is to be made available at all points. There can actually be multiple use cases like having one Log object for each network request or one Log object per session. And the plumbing required to make this work should be fitted sideways without the application programmer bothering about it. He just knows that that execution context is available at all places.

I think it should fairly common problem faced by everyone so please give me some ideas.

Following is the problem

MainServer.js


  app = require('express').createServer();
  app_module1 = require('AppModule1');
  var session = get_session();
  app.get('/my/page', app_module1.func1);

AppModule1.js

  app_module2 = require('AppModule2');
  exports.func1 = function(req,res){

     //  I want to know which the session context this code is running for

     app_module2.func2(req,res);

   }

AppModule2.js

   exports.func2 = function(req,res){

    // I want to know where the session context in which this code is running

    }

解决方案

You can achieve this using Domains -- a new node 0.8 feature. The idea is to run each request in it's own domain, providing a space for per-request data. You can get to the current request's domain without having to pass it all over via process.domain.

Here is an example of getting it setup to work with express: How to use Node.js 0.8.x domains with express?

Note that domains in general are somewhat experimental and process.domain in particular is undocumented (though apparently not going away in 0.8 and there is some discussion on making it permanent). I suggest following their recommendation and adding an app-specific property to process.domain.data.

https://github.com/joyent/node/issues/3733

https://groups.google.com/d/msg/nodejs-dev/gBpJeQr0fWM/-y7fzzRMYBcJ

这篇关于Node.js用于获取执行上下文的编程模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆