简体   繁体   中英

understand the connect-mongo usage, how to use connect-mongo for sessions

I'm in a node.js app, and I'm trying to get the session data to store in a mongoDB database.

But the I dont understand the usage of connect-mongo and I would like if someone could explain to me the correct usage and what I can't connect to my mongoDB db.

my app.js code is

var config = require('./config')
    , express = require('express')
    , passport = require('passport')
    , site = require('./site')
    , oauth2 = require('./oauth2')
    , user = require('./user')
    , client = require('./client')
    , token = require('./token')
    , https = require('https')
    , fs = require('fs')
    , path = require('path')
    , mongoose = require('mongoose')
    , MongoStore = require('connect-mongo')(express);

var sessionStorage;

    var MongoStore = require('connect-mongo')(express);
    console.log('Using MongoDB for the Session');
    sessionStorage = new MongoStore({
        db: config.session.dbName
    });

var app = express();
app.set('view engine', 'ejs');
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.urlencoded());
app.use(express.json());


//Session Configuration
app.use(express.session({
    secret: config.session.secret,
    store: sessionStorage,
    key: "authorization.sid",
    cookie: {maxAge: config.session.maxAge }
}));

app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(function(err, req, res, next) {
    if(err) {
        res.status(err.status);
        res.json(err);
    } else {
        next();
    }
});
require('./auth')(passport); 

app.get('/', site.index);
app.get('/login', site.loginForm);
app.post('/login', site.login);
app.get('/logout', site.logout);
app.get('/account', site.account);

app.get('/dialog/authorize', oauth2.authorization);
app.post('/dialog/authorize/decision', oauth2.decision);
app.post('/oauth/token', oauth2.token);

app.get('/api/userinfo', user.info);
app.get('/api/clientinfo', client.info);

app.get('/api/tokeninfo', token.info);

app.use(express.static(path.join(__dirname, 'public')));

mongoose.connect('mongodb://user:pass@host:10026/dbName');

var db = mongoose.connection;

db.on('error',console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
    console.log("Connected to db");
});

mongoose.set('debug', true);

var options = {
    key: fs.readFileSync('certs/privatekey.pem'),
    cert: fs.readFileSync('certs/certificate.pem')
};

https.createServer(options, app).listen(3000);
console.log("Authorization Server started on port 3000");

and output error is

authorization-server\node_modules\connect-mongo\lib\connect-mongo.js:155
          throw new Error('Error connecting to database <' + err + '>');
                ^
Error: Error connecting to database <Error: failed to connect to [127.0.0.1:27017]>

Notice in your error message mongoose is trying to connect to the localhost at the default port. Also you need to define your db connection at the top of your file.

Change:

mongoose.connect('mongodb://user:pass@host:10026/dbName');

To:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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