处理并保留node.js / express会话 [英] Deal and keep node.js/express sessions

查看:151
本文介绍了处理并保留node.js / express会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个网络应用程序,这个网络应用程序每次调用 fs.mkdir ,删除所有当前的快速会话,所以我需要一种方法保持所有这些会议。我试图用connect-mongodb和connect-redis保持所有这些会话,但是两者都不起作用,nodemon总是说req.session是未定义的。我不知道我要做什么来保持所有的会议。

I'm currently working on a web app, this web app every time fs.mkdir is called, delete all the current express sessions, so I need a way to keep all this sessions. I was trying to keep all this sessions with connect-mongodb and connect-redis, but neither works, nodemon always says that req.session is undefined. I don't know what I have to do to keep all the session.

我需要一种方法来保留所有会话,在执行 fs.mkdir 时不要丢失它们,和它的教程,因为我还没有找到任何好的和完整的教程,任何人都可以!在Stackoverflow中,我正在阅读所有与此相关的内容,但是一切正常!请帮忙!

I need a way to keep all the sessions, don't lose them when fs.mkdir is executed, and a tutorial for it, because I don't found any good and complete tutorial for this yet, anyone works! I was reading all releated with this here, in Stackoverflow, but anything works! Please help!

推荐答案

这听起来很奇特,在 fs.mkdir 你的会话被删除了。 (我发现了你的另一个问题)

it sounds very exotic, that after fs.mkdir your session erased. (I found your other SO question)

但回到这个问题,这里有一些代码片段,你可以如何使用会话(redis)。

But back to the question, here's a little code snippet how you can use sessions (redis).

var express = require('express')
  , fs = require('fs')
  , http = require('http')
  , RedisStore = require('connect-redis')(express)
  , sessionStore = new RedisStore()
  , app = express()
  ;

app.configure(function(){
  app.set('port', 3000);
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('secret'));
  app.use(express.session({
    store: sessionStore
  }));
  app.use(express.static(__dirname + '/public'));
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.get( '/', function(req, res){
  res.setHeader("Content-Type", "text/html");
  console.log( req.session );
  console.log( '------------------------' );
  res.end( 'root' );
});

app.get( '/dir/:id', function(req, res){
  console.log( req.session );
  console.log( '------------------------' );

  fs.mkdir( req.params.id );
  if ( req.session.dirs === undefined )
    req.session.dirs = [];
  req.session.dirs.push( req.params.id );

  console.log( req.session );
  console.log( '------------------------' );

  res.setHeader("Content-Type", "text/html");
  res.end( 'dir' );
});


server = http.createServer(app).listen(3000);

你应该阅读表达,并浏览示例快递

you should read the docs of express, and browse the examples of express

这篇关于处理并保留node.js / express会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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