2 tiantangmunaiyi tiantangmunaiyi 于 2017.09.18 12:45 提问

connect-redis Error: Can't set headers

再用connect-redis module存储session到redis 但是报错Error: Can't set headers after they are sent. 找了半天都没找到什么问题,我快崩溃了,有没有人遇到过?
我用的是express 4x redis 2.1.x ,这是我的代码:

 var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var redis = require('redis');
var RedisStore = require('connect-redis')(session);
var serveStatic = require('serve-static');
var login = require('./routes/login');
//var users = require('./routes/users');
var handle_login = require('./routes/handle_login');
var flickr_images = require('./routes/flickr_images');
var logout = require('./routes/logout');
var handle_upload = require('./routes/handle_upload');
var share_result = require('./routes/share_result');

var forward_flickr = require('./routes/forward_flickr');

//console.log(__dirname+'\\public');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');
app.set('view engine','ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
    secret:'my app secret',
  store:new RedisStore({
    host:'127.0.0.1',
    port:'6379'
    //logErrors:true
  })
}));

app.use(serveStatic(__dirname+'\\public'));
//app.use(express.static(path.join(__dirname, '/public')));

app.use('/', login);
app.use('/login',login);
//app.use('/users', users);
app.use('/handle_login',handle_login);
app.use('/logout',logout);
app.use('/handle_upload',handle_upload);
app.use('/share_result',share_result);
app.use('/flickr_images',flickr_images);
app.use('/forward_flickr',forward_flickr);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
Csdn user default icon
上传中...
上传图片
插入图片