Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js Error: Most middleware (like session) is no longer bundled with Express and must be installed separately [closed]

Tags:

node.js

I have just upgraded to Express version 3 and I am seeing an error in my middleware. Specifically:

Error: Most middleware (like session) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.

The stack trace is:

at Function.Object.defineProperty.get (/home/phpsaravana/nodeshop/node_modules/express/lib/express.js:89:13)
at module.exports (/home/phpsaravana/nodeshop/node_modules/connect-mongo/lib/connect-mongo.js:30:39)
at Object.<anonymous> (/home/phpsaravana/nodeshop/admin.js:6:42)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3

How do I fix this?

like image 377
Saravanan Avatar asked Nov 19 '14 12:11

Saravanan


2 Answers

In newer versions of express, middle-wares like session are not bundled with express. Furthermore, if you want to use them, then you have to install them separately. like :

 npm install express-session

and then require it :

var session = require('express-session');

and then use this:

app.set('trust proxy', 1) // trust first proxy
app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true }
}))
like image 166
Mohammad Rahchamani Avatar answered Sep 22 '22 20:09

Mohammad Rahchamani


I think your problem is about upgrading from express 2 to 3. In Express 3, most of the package previously bundled with them, are now single package that you need to require, in your app.

Of course you need to include it in your package.json and do the npm install as usual.

var express = require('express')
var session = require('express-session')

var app = express()

app.use(session({secret: 'keyboard cat'}))

See: ExpressJS / Session

like image 33
Kangcor Avatar answered Sep 19 '22 20:09

Kangcor