在 Node.js 中管理会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3737062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Managing Sessions in Node.js?
提问by Timmy
What is the best way to manage session variables in Node.js? Is there any library?
在 Node.js 中管理会话变量的最佳方法是什么?有图书馆吗?
采纳答案by donald
You can do that very easily using:
你可以很容易地使用:
Connect: http://senchalabs.github.com/connect/
Connects is like Rack in Ruby. It gives you an extra layer where you can "play" with authentication, sessions, cookies, among others.
连接:http: //senchalabs.github.com/connect/
Connects 就像 Ruby 中的 Rack。它为您提供了一个额外的层,您可以在其中“玩”身份验证、会话、cookie 等。
Other option is to use frameworks:
其他选择是使用框架:
Express.js: http://expressjs.com/
It seems to be the most used node.js framework. Is like Sinatra for Ruby and runs on top of connect.
Geddy: http://geddyjs.org/
If you want to do more complex WebApps, Geddy is the one you choose. Is like Rails for Ruby.
Express.js:http: //expressjs.com/
它似乎是最常用的 node.js 框架。就像 Ruby 的 Sinatra 一样,运行在 connect 之上。
格迪:http: //geddyjs.org/
如果您想做更复杂的 Web 应用程序,Geddy 就是您的选择。就像 Ruby 的 Rails。
回答by z5h
nodejs provides a basic http API. http is stateless, and ideas of sessions and session variables exist in framework/protocols build on top of http. http://en.wikipedia.org/wiki/Session_%28computer_science%29
nodejs 提供了一个基本的 http API。http 是无状态的,会话和会话变量的想法存在于构建在 http 之上的框架/协议中。http://en.wikipedia.org/wiki/Session_%28computer_science%29
Take a look at http://geddyjs.org/or http://expressjs.com/as examples of web frameworks built with node that provide sessions.
查看http://geddyjs.org/或http://expressjs.com/作为使用提供会话的节点构建的 Web 框架的示例。
回答by WeNeedAnswers
Just offload it to memcache or some other caching mechanism. I wouldn't burden your servers with this sort of thing. What is the point of a super lean web server that has to remember stuff.
只需将其卸载到内存缓存或其他一些缓存机制。我不会用这种事情给你的服务器带来负担。必须记住内容的超级精益 Web 服务器有什么意义。
I would also try and develop your site as an application and not a website, or treat your website as an application, use the wonderful features of html5 such as local storage/local databases and cut down on the amount of traffic between server and client machines.
我也会尝试将您的网站开发为应用程序而不是网站,或者将您的网站视为应用程序,使用 html5 的精彩功能,例如本地存储/本地数据库,并减少服务器和客户端计算机之间的流量.
If all else fails (or site is small) then what's stopping you write your own session class. Not that difficult. Especially if its an in memory type thing. Put some timer logic to time out sessions and there you go. Damn in a dynamic language such as JavaScript, should be a cinch.
如果所有其他方法都失败(或站点很小),那么是什么阻止了您编写自己的会话类。没那么难。特别是如果它是内存类型的东西。放置一些计时器逻辑来超时会话,然后就可以了。该死的动态语言,如 JavaScript,应该是小菜一碟。
Structure should be a dictionary with key being session and value being an object containing details of last communication and capabilities (to enable access to certain features). Add a sweep function to clear out old sessions that have timed out. and bingo. A basic session service. a basic check on "is session key in list...yes/no...get details"...and I think thats it....or is there some feature that I am missing.
结构应该是一个字典,键是会话,值是一个对象,包含上次通信和功能的详细信息(以允许访问某些功能)。添加清除功能以清除已超时的旧会话。和宾果游戏。一个基本的会话服务。对“列表中的会话密钥...是/否...获取详细信息”的基本检查...我认为就是这样....或者是否有一些我遗漏的功能。
I personally would avoid any third party tool out there for as long as possible. Sands of time shift very quickly and you can always depend on code developed by yourself.
我个人会尽可能避免使用任何第三方工具。时间变化非常快,您始终可以依赖自己开发的代码。
回答by martyn
Donald's answer is good - once you get into the onion pattern of connect middleware you have to make a decision on what type of session store to use. The default one in express is a MemoryStore, and is not intended for production use. Here are some of your choices:
Donald 的回答很好——一旦你进入了连接中间件的洋葱模式,你就必须决定使用哪种类型的会话存储。express 中的默认值是 MemoryStore,不用于生产用途。以下是您的一些选择:
Mongohttps://github.com/mikkel/express-session-mongo- Be sure to use the option 'native_parser:false'
Mongo https://github.com/mikkel/express-session-mongo- 请务必使用选项“native_parser:false”
Redishttps://github.com/visionmedia/connect-redis- Very good, but if you aren't already using redis for pub/sub or storage it might not be ideal.
Redis https://github.com/visionmedia/connect-redis- 非常好,但如果您还没有将 redis 用于发布/订阅或存储,它可能并不理想。
Note, there are other choices - it depends on your project. Look for something you can introduce leveraging your existing technology stack.
请注意,还有其他选择 - 这取决于您的项目。寻找可以利用现有技术堆栈引入的东西。
回答by Hage Yaapa
If you are looking for serious web development using Node.js, use Express framework; it supports sessions.
如果您正在寻找使用 Node.js 进行严肃的 Web 开发,请使用Express 框架;它支持会话。
Create the Express project with the --sessions options.
使用 --sessions 选项创建 Express 项目。
$ express --sessions
To install Express:
要安装 Express:
$ npm install express -g

