我正在制作一个简单的网络应用,并尝试添加身份验证。一切都工作正常(单独),直到我尝试将身份验证页面与应用程序合并,并收到此错误消息:
" TypeError:无法读取属性' csrfSecret'未定义的 Object.handle(C:项目\ node_modules \表达\ node_modules \连接\ node_modules \ csurf \ index.js:46:29)..."
这是app.js中的代码,也许你们中的一个人会看到问题的根源在哪里。
var flash = require('connect-flash');
var express = require('express');
var app = express();
app.use(function(req, res, next) {
req.db = {};
req.db.tasks = db.collection('tasks');
next();
})
app.locals.appname = 'Name of App'
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var routes = require('./routes');
var tasks = require('./routes/tasks');
var http = require('http');
var path = require('path');
var mongoskin = require('mongoskin');
var db = mongoskin.db('connection works perfectly to my mongolab account', {safe:true});
var configDB = require('./config/database.js');
mongoose.connect(configDB.url);
require('./config/passport')(passport);
app.configure(function() {
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.methodOverride());
app.use(express.csrf());
app.use(require('less-middleware')({ src: __dirname + '/public', compress: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res, next) {
res.locals._csrf = req.session._csrf; //THIS COULD BE THE SOURCE OF THE PROBLEM
return next();
})
app.use(app.router);
app.use(express.session({ secret: 'Bigsecretscretsecret' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
});
require('./app/routes.js')(app, passport);
app.listen(port);
console.log('Go to' + port);
答案 0 :(得分:1)
使用Express.js 4.x,连接中间件已分解为单个模块。确保安装:
$ npm install --save express-session
&安培;
$ npm install --save cookie-parser
添加到 app.js 文件中:
var cookieParser = require('cookie-parser');
var session = require('express-session');
app.use(cookieParser())
app.use(session({ secret: 'keyboard cat', key: 'sid', cookie: { secure: true }})
查看文档express-session