Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

使用connect-redis模块出现错误

$
0
0

我要用redis保存session,用了connect-redis app.use(session({ store:new RedisStore({ host: ‘localhost’, port: 6379, }), secret:‘example’, }));

但是会出现error Can’t set headers after they are sent 把store注释了就不会提示错误 而是提示warning 要我使用session-store,怎么破?

app.js 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 flash=require(‘connect-flash’); var mysql=require(“mysql”); var multiparty=require(‘multer’); var RedisStore = require(‘connect-redis’)(session); //var routes = require(’./routes/index’); //var users = require(’./routes/users’); var app = express();

// view engine setup app.set(‘views’, path.join(__dirname, ‘/app/views’)); app.engine(’.html’, require(‘ejs’).__express); app.set(‘view engine’, ‘html’); // 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(express.static(path.join(__dirname, ‘/app/views/static’))); app.use(session({ store:new RedisStore({ host: ‘localhost’, port: 6379, }), secret:‘example’, cookie:{maxAge:60000}})); app.use(flash()); app.use(function (req,res,next) { res.locals.user=req.session.user; var error = req.flash(‘error’); res.locals.error = error.length ? error : null; var success = req.flash(‘success’); res.locals.success = success.length ? success : null; next(); }); var routes = require(’./config/routes’)(app); /app.use(’/’, routes); app.use(’/users’, users);/ // 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 handlers

// development error handler // will print stacktrace if (app.get(‘env’) === ‘development’) { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render(‘error’, { message: err.message, error: err }); }); }

// production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render(‘error’, {message: err.message,error: {}}); });

module.exports = app;


Viewing all articles
Browse latest Browse all 14821

Trending Articles