windows 10上的redis erreur with express

kcwpcxri  于 2021-06-09  发布在  Redis
关注(0)|答案(0)|浏览(234)

我想在windows上使用redis和express和passport。但当我提出任何请求时,我得到了一个错误:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:526:11)
    at ServerResponse.header (C:\Users\alpaga\Documents\Documents\Programmation\Express-Passport-Redis\Main\node_modules\express\lib\response.js:767:10)
    at ServerResponse.send (C:\Users\alpaga\Documents\Documents\Programmation\Express-Passport-Redis\Main\node_modules\express\lib\response.js:170:12)
    at done (C:\Users\alpaga\Documents\Documents\Programmation\Express-Passport-Redis\Main\node_modules\express\lib\response.js:1004:10)
    at Object.exports.renderFile (C:\Users\alpaga\Documents\Documents\Programmation\Express-Passport-Redis\Main\node_modules\pug\lib\index.js:412:12)
    at View.exports.__express [as engine] (C:\Users\alpaga\Documents\Documents\Programmation\Express-Passport-Redis\Main\node_modules\pug\lib\index.js:455:11)
    at View.render (C:\Users\alpaga\Documents\Documents\Programmation\Express-Passport-Redis\Main\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\alpaga\Documents\Documents\Programmation\Express-Passport-Redis\Main\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\alpaga\Documents\Documents\Programmation\Express-Passport-Redis\Main\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\alpaga\Documents\Documents\Programmation\Express-Passport-Redis\Main\node_modules\express\lib\response.js:1008:7)

当我在linux上用相同的代码使用redis时,我没有这个错误(我的会话不是保存在windows上的redis中,而是在linux上)。
其他问题我可以在linux上登录并保存会话,但是当我用passport检查auth时,函数返回的总是false。

var createError = require('http-errors');
var express = require('express');
const session = require('express-session');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const redis = require('redis');
const redisClient = redis.createClient({
  host: 'localhost',
  port: 6379
});
const redisStore = require('connect-redis')(session);
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;

var app = express();

redisClient.on('error', (err) => {
  console.log('Redis error: ', err);
});

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

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser('hey You'));
app.use(express.static(path.join(__dirname, 'public')));

app.use(passport.initialize());
app.use(passport.session());
app.use(session({
  secret: 'hey You',
  resave: false,
  saveUninitialized: true,
  cookie: {secure: true, maxAge:86400000},
  store: new redisStore({client: redisClient}),
}));

app.get('/success', (req, res) => res.render('index', { title: 'OK' }));
app.get('/failure', (req, res) => res.render('index', { title: 'KO' }));
app.post('/login', passport.authenticate('local', {
  successRedirect: '/success',
  failureRedirect: '/failure',
  failureFlash: true
}));

app.get('/connected', (req, res, next) => {
  console.log(req.session);
  if (req.isAuthenticated()) {
    return res.render('index', { title: 'Connected' });
  }
  return res.render('index', { title: 'Not Connected' })
});

passport.use(new LocalStrategy({usernameField: 'email'}, 
    async (email, password, done) => {
      return done(null, {id: 1, email: 'eliott.martin@epitech.eu'});
    }
));
passport.serializeUser((user, done) => {
  console.log(user);
  return done(null, user.id)
});
passport.deserializeUser((id, done) => {
  console.log(id);
  return done(null, {id: 1, email: 'eliott.martin@epitech.eu'});
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// 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;

如果有人知道怎么修的话!
谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题