node.js - NodeJS Mongoose connect-mongo Session Storage with Mongo -
i seek configure connect-mongo in way can utilize session in nodejs persisted mongo. utilize next code
var mongoose = require('mongoose'); function connect(url, callback) { mongoose.connect(url); var connection = mongoose.connection; connection.on('error', console.error.bind(console, 'connection error:')); connection.once('open', function() { console.log("mongoose connected at: ", url); callback(connection); }); } var express = require("express"); var body_parser = require('body-parser'); var cookie_parser = require('cookie-parser'); var hogan_express = require('hogan-express'); var session = require('express-session'); var mongo_store = require('connect-mongo')(session); var express = require("express"); var app = express(); app.engine('html', hogan_express); app.set('view engine', 'html'); app.set("views", "views"); app.use("/libs", express.static("bower_components")); if (!config.development) { app.use(express.static("min")); } app.use(express.static("public")); connect("mongodburl",function(mongoose_connection){ app.use(body_parser.json()); app.use(cookie_parser()); app.use(session({ secret: "asd", store: new mongo_store({ mongoose_connection: mongoose_connection // db: mongoose_connection.db }) })); })
i have tried executing code have never object session in res refrence.
getting: typeerror: cannot set property 'asd' of undefined
code:
app.get("/rest/test",function(req, res) { req.session.asd="test"; res.send(req.session.asd) });
somone give hint solution?
you need define routes after configure session.
... app.use(session.... ... app.get("/...
inside connect's callback in case. start listening within well.
connect("mongodburl",function(mongoose_connection){ app.use(body_parser.json()); app.use(cookie_parser()); app.use(session({ secret: "asd", store: new mongo_store({ mongoose_connection: mongoose_connection // db: mongoose_connection.db }) })); app.get("/rest/test",function(req, res) { req.session.asd="test"; res.send(req.session.asd) }); app.listen(... });
node.js express connect-mongo
No comments:
Post a Comment