问题描述
我需要一些快递方面的帮助:我在express中有一个名为lastQuestion的变量,即使在跨多个函数分配时也要记住最后一个问题.
I need some help with express:I have a variable in express that is called lastQuestion, to remember the last question even when assigned across multiple functions.
这是我的代码的简化版本:
here's a simplified version of my code:
var lastQuestion;
function sjhda(){
//uses the variable
lastQuestion = generateQuestion();
return(lastQuestion);
}
function asjhasd(){
//uses the variable
lastQuestion = generateQuestion();
return(lastQuestion);
}
function asjhd(){
//uses the variable
lastQuestion = generateQuestion();
return(lastQuestion);
}
假设有两个客户正在使用该网站,并且发生这种情况:
Let's say that two clients are using the website and this happens:
1:客户端一开始使用漫游器,并开始回答问题(以及变量lastQuestion!).2:已经在的客户2产生一个新问题,并开始回答该问题(在重置lastQuestion的同时),死亡几率是多少?"3:客户一个提交答案很棒".现在回答问题死亡几率是多少?"已被保存为伟大".我怎样才能解决这个问题?另外,如果可能的话,我也希望避免使用Cookie.谢谢!
1: client one starts using the bot, and begins to answer the question (and variable lastQuestion!) "How are you?"2: client two who is already on generates a new question, and begins to answer the question (while resetting lastQuestion), "What are the odds of death?"3: client one submits the answer "great". now the answer to the question "What are the odds of death?" has been saved as "great".How can I fix this? Also, I'd like to avoid using cookies if possible. Thanks!
推荐答案
这就是您要使用的 express-session .然后,您可以代表一个特定用户保存状态,并且状态也不会与使用同一服务器的其他用户混淆.
This is what you would use express-session for. You can then save state on behalf of one particular user and it won't get mixed up with other users also using the same server.
基本思想是,当用户首次访问您的服务器时,快速会话代码会在其上放置一个cookie,然后浏览器将根据该特定浏览器的所有将来请求将其发送回给您.该Cookie包含一个uniqueID,该ID会在对服务器的后续请求中标识该特定客户端.
The basic idea is that when a user comes to your server for the first time, the express-session code drops a cookie on them that the browser will then send back to you on all future requests from that particular browser. That cookie contains a uniqueID that identifies that particular client on subsequent requests to your server.
然后,快速会话代码也可以创建与该特定会话ID(存储在Cookie中)相关联的存储对象.快速会话有很多不同的存储驱动程序,用于确定实际存储的工作方式(数据库,内存,redis等).大多数人都开始使用内置内存存储并运行,然后在以后是否需要将其移至用于会话数据的持久存储上进行处理.
Then, the express-session code can also create a stored object that is associated with that particular session id (that is stored in the cookie). There are lots of different storage drivers for express-session to determine how the actual storage works (database, memory, redis, etc...). Most people get up and running with the built-in memory store and then work on it later whether they need to move to a persistent store for the session data.
现在,有一个对象与您网站上的每个用户唯一关联.然后,您的Express路由可以读取数据并将数据写入该对象,从该对象读取属性或向该对象添加新属性,或者在现有属性上设置值.通过编程,您就像在 req.session
中的Javascript对象一样访问它.
Now, there's an object that is uniquely associated with each user that comes to your site. Your Express routes can then read and write data to that object, either reading properties from it or adding new properties to it or setting values on existing properties. Programmatically, you just access it like a Javascript object in req.session
.
如果您的站点具有登录过程,则该会话可以唯一地绑定到特定的登录用户,因此相同的会话数据甚至可以跟随给定的登录用户从一个设备到另一个设备.如果没有登录,则会话仅适用于用户正在使用的特定浏览器,该浏览器通常在较短类型的会话持续时间内正常运行,而用户在操作过程中不会切换设备.
If your site has a login process, then the session can be uniquely tied to a particular logged in user so the same session data will even follow a given loggedin user from one device to the next. If there's no login, then the session only applies to a particular browser that the user is using which typically works fine for shorter-type session durations where the user doesn't switch devices in the middle of the operation.
仅供参考,在任何Node.js服务器中,都不应将特定用户的状态存储在公共,共享模块级别或全局变量中,因为(如您所知),多个用户将发生冲突,覆盖或继承其他用户的数据用户.这就是为什么所有这样的数据都应该由一个唯一的索引建立索引,该标识符仅与该用户或客户端有关,并且只能通过该索引进行访问.
FYI, in any nodejs server, you should never store a particular user's state in a common, shared module level or global variable because (as you seem to already know), multiple users will collide, overwriting or inheriting the data from other users. That's why all data like this should be indexed by some unique identifier that pertains to just that user or client and only accessed through that index.
使用express-session的简单示例如下:
A simple example using express-session would look like this:
const express = require('express');
const app = express();
const session = require('express-session');
app.use(session({ secret: 'keyboard cat'}))
app.get("/question", (req, res) => {
console.log(req.session.lastQuestion);
if (!req.session.lastQuestion) {
// send first question
res.send(...);
} else {
// use req.session.lastQuestion to determine which to send next
res.send(...);
}
// update session state to represent the question we just sent
req.session.lastQuestion = questionWeJustDid;
});
app.listen(80);
这篇关于在Express中本地化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!