问题: 当我尝试使用session = require(“express-session”)时,安装快速会话时出错,但我已经使用npm install express-session安装了它。我收到的错误是:
vishal@rocker:~/clg/project1/node_modules/express$ nodejs server.js
Error: Most middleware (like session) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
at Function.Object.defineProperty.get (/home/vishal/clg/project1/node_modules/express/lib/express.js:89:13)
at Object.<anonymous> (/home/vishal/clg/project1/node_modules/express/server.js:17:17)
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:902:3
现在安装快速会话后,我的目录结构是
node_modules
express
server.js // file where i am using it
express-session
Server.js
var express = require("express"),
http = require("http"),
session = require("express-session"),
login = require("./server/login"),
app;
app = express();
http.createServer(app).listen(3000);
// set up a static file directory to use for default routing
app.use(express.static(__dirname+"/client"));
// set up express session to create session when user successfully login
app.use(express.session({secret: '123456'}));
// set up our routes
// for user login
app.get("/login", function (req, res) {
var result = login.validate(req,res);
if(result){
req.session.username="admin";
res.send("successfully logged in");
}
else{
res.append("error","Authentication Failed");
res.sendFile(__dirname+"/client/index.html");
}
});
app.get("/goodbye", function (req, res) {
res.send("Goodbye World!");
});
我的新package.json
{
"name": "askr",
"version": "0.0.1",
"description": "Class project for 473",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD-2-Clause",
"dependencies": {
"express-session": "~1.10.3",
"express": "~4.12.2"
}
}
答案 0 :(得分:0)
express.session 直接使用会话折旧。如果在使用其他模块时遇到此错误,请确保已正确提及方法名称。