Express.js:app.locals vs req.locals vs req.session [英] Express.js: app.locals vs req.locals vs req.session

查看:337
本文介绍了Express.js:app.locals vs req.locals vs req.session的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力了解何时最好使用以下每一项。这是我的初步理解:

I'm trying to understand when it's best to use each of the following. Here is my rudimentary understanding:

app.locals - 适用于在应用级别存储全局变量。所有用户/会话将看到与这些变量相同的值。变量可用于所有视图。

app.locals -- good for storing global variables at the app level. all users/sessions will see the same values for these variables. the variables are available to all views.

res.locals - 适用于存储特定请求/响应周期的变量。变量仅适用于与响应相关联的视图。

res.locals -- good for storing variables for the specific request/response cycle. the variables are only available to the view associated with the response.

req.session - 适用于存储与唯一用户会话关联的变量(例如用户名)。这些变量应该可用于唯一用户/会话的所有视图。

req.session -- good for storing variables associated with the unique user session (e.g., user name). these variables should be available to all views for the unique user/session.

具体的用例如下:用户运行查询,从mongodb检索数据。我现在想要这个查询的结果,这是一个json数组,可以作为所有视图(http请求)的变量。什么是存储结果数组以便每个视图可以访问的最佳方法?

The specific use case I have is as follows: A user runs query which retrieves data from mongodb. I now want the result of this query, which is a json array, available as a variable to ALL of the views (http requests). What's the best way to "store" the result array so that each view can access it?

谢谢

推荐答案


我现在想要这个查询的结果,这是一个json数组,
可用作所有视图的变量。
最好的方式来存储结果数组,以便每个视图都可以访问它?

I now want the result of this query, which is a json array, available as a variables to ALL of the views. What's the best way to "store" the result array so that each view can access it?

当你说可用于所有的视图我认为你的意思是跨所有HTTP请求。如果是这种情况,那么您需要注意HTTP是一种无状态协议,不提供此功能。您需要为此开发自己的机制。

When you say "available to ALL of the views" I assume you mean across all HTTP requests. If that is the case then you need to be aware that HTTP is a stateless protocol and does not provide for this. You'll need to develop your own mechanism for this.

一种方法是通过在服务器上缓存此信息(数组),并在每个请求上检索(例如,从内存而不是从MongoDB检索它) )。您将在Cookie上存储会话ID,并根据此ID从另一请求通过时从缓存中提取该ID。有几种可用的缓存工具(例如redis,memcached等),您可以选择将信息存储在内存中。

One way of doing this is by cacheing this information (the array) on the server and retrieve it on every request (for example, retrieve it from memory rather than from MongoDB). You'll store a session ID on the cookie and based on this ID fetch it from cache when another requests comes through. There are several cache tools available (e.g. redis, memcached, et cetera) that you can chose to store the information in memory.

您还可以将此信息(数组自身),在这种情况下,它将在每个HTTP请求之间在客户端和服务器之间来回发送,除非数据很小,否则很有可能不是一个好主意。

You could also cookie this information (the array itself) in which case it will be send back and forth between the client and the server on every HTTP request and very likely won't be a very good idea unless the data is very small.

这篇关于Express.js:app.locals vs req.locals vs req.session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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