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

查看:244
本文介绍了Express.js:app.locals与res.locals与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 -- 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检索).您将会话ID存储在Cookie上,并在其他请求通过时基于该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与res.locals与req.session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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