gpt4 book ai didi

javascript - 运行 node.js 服务器端脚本时出错

转载 作者:行者123 更新时间:2023-11-30 12:13:09 26 4
gpt4 key购买 nike

尝试使用 node.js 使用命令 node server 运行 Node 服务器时出现以下错误。

Error:

express-session deprecated undefined resave option; provide resave option server
.js:18:9
express-session deprecated undefined saveUninitialized option; provide saveUnini
tialized option server.js:18:9
C:\xampp\htdocs\cit_node\node_modules\express\lib\router\route.js:196
throw new Error(msg);
^

Error: Route.get() requires callback functions but got a [object Undefined]
at Route.(anonymous function) [as get] (C:\xampp\htdocs\cit_node\node_module
s\express\lib\router\route.js:196:15)
at EventEmitter.app.(anonymous function) [as get] (C:\xampp\htdocs\cit_node\
node_modules\express\lib\application.js:481:19)
at Object.<anonymous> (C:\xampp\htdocs\cit_node\server.js:20:5)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:134:18)
at node.js:961:3

我在下面解释我的服务器端代码。

server.js:

var port=8888;
var express=require('express');
var http=require('http');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var expressSession = require('express-session');
var cookieParser = require('cookie-parser'); // the session is stored in a cookie, so we use this to parse it
var app = express();
var server=http.createServer(app);
var admin=require('./route/route.js');
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded
app.use(bodyParser.json()) // parse application/json
app.use(methodOverride()); // simulate DELETE and PUT
app.use(cookieParser());
app.use(expressSession({secret:'abcx'}));
app.post('/login',admin.adminlogin);
app.get('/session',admin.session);
//app.post('/logout',admin.logout);
app.get('/',function(req,res){
res.sendfile('view/index.html');
});
server.listen(port);
console.log('Magic happens on port'+port); // shoutout to the user

之前我能够运行它但不知道为什么会抛出这种类型的错误。我的 package.json 文件在下面给出。

{
"name": "Gofasto",
"main": "server.js",
"dependencies": {
"express": "4.13.3",
"morgan": "1.6.1",
"body-parser": "1.14.1",
"method-override": "2.3.5",
"socket.io":"1.3.7",
"mongojs":"1.4.1",
"cookie-parser": "1.4.0",
"express-session": "1.11.3"
}
}

route.js:

var mongo = require('mongojs');
var database='go_fasto';
var collections=['user'];
var db=mongo("127.0.0.1:27017/"+database, collections);
exports.adminlogin=function(req,res){
var username=req.body.username;
var password=req.body.password;
db.user.findOne({
user_name:username,
raw_password:password
},function(err,docs){
if(!err){
if(docs){
console.log('docs',docs);
ses=req.session;
ses.username=docs.user_name;
ses.user_type=docs.user_type;
ses.email_id=docs.email;
ses.dept_id=docs.dept_id;
res.send(docs);
}
}
if(err){
res.send("could not login");
}
});
}
/*exports.logout=function(req,res){
req.session.destroy(function(err){
if(err){
console.log(err);
}
else
{
res.redirect('/');
}
});
}
*/

我正在使用 Node 版本 4.2.1。请帮我解决这个错误。

最佳答案

根据评论看这个:https://github.com/expressjs/session/issues/56

您需要指定 resavesaveUninitialized 选项:

app.use(expressSession({
secret: 'abcx',
resave: true,
saveUninitialized: true
}));

那么错误就是指这行代码:

app.get('/session',admin.session);

因为 admin.session./route/route.js 中未定义

关于javascript - 运行 node.js 服务器端脚本时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33199902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com