简体   繁体   English

如何在connect-mongo中使用会话?

[英]How to use sessions with connect-mongo?

In docs of connect-mongo I read only about it set up, nothing more. connect-mongo文档中,我仅阅读有关它的设置的信息,仅此而已。 How to define sessions? 如何定义会话? How to read? 如何阅读?

const mongoose = require("mongoose");
mongoose.Promise = Promise;
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);

mongoose.connect('mongodb://localhost/MYDATABASE');

app.use(session({
    secret: "SOME_SECRET_KEY",
    store: new MongoStore({ mongooseConnection: mongoose.connection })
}));

Okay, I set up. 好吧,我准备好了。 If I have 如果我有

app.get("/login", function(req, res){
// If user authorized
// I want to define a session.user = req.body.user 
// And then I want to read this value in other my site pages
});

How I can define user login and some other data to session? 如何定义用户登录名和其他要会话的数据?

How I can read this values? 如何读取此值?

Where this session will store in MongoDB? 该会话将存储在MongoDB中的何处? Or I need to define not only way to MYDATABASE and to MYDATABASE/sessionstore ? 还是我不仅需要定义通往MYDATABASE和通往MYDATABASE / sessionstore的方式

Must I to generate secret or this must be a one defined string? 我必须生成秘密,还是必须是一个定义的字符串?

  1. Reading from and writing to the session is done through the req.session object: req.session.userId = req.body.userId 通过req.session对象完成对会话的读取和写入: req.session.userId = req.body.userId

  2. The session data will be stored in a collection called sessions by default. 默认情况下,会话数据将存储在称为sessions的集合中。

  3. About the session secret 关于会话机密

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM