我无法将会话传递到下一条路由。我存储了会话并记录了该会话,它在那里,但是当我将其重定向到下一个视图时,它消失了。
这是我的app.js
// require modules
const createError = require('http-errors');
const express = require('express');
const cookieSession = require('cookie-session');
const flash = require('express-flash');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const bodyParser = require('body-parser');
const appRoot = require('app-root-path');
const session = require('express-session');
// require routers
const indexRouter = require('./routes/index');
const facturasRouter = require('./routes/facturas');
const dashboardRouter = require('./routes/dashboard');
const rendicionesRouter = require('./routes/rendiciones');
const perfilRouter = require('./routes/perfil');
const loginRouter = require('./routes/login');
const logoutRouter = require('./routes/logout');
const registerRouter = require('./routes/register');
const usuariosRouter = require('./routes/usuarios');
const tipocomprobanteRouter = require('./routes/tipocomprobante');
const tipogastoRouter = require('./routes/tipogasto');
const monedaRouter = require('./routes/moneda');
const app = express();
// set global variable root path
app.locals.rootPath = 'http://localhost:3000/';
// 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(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); // parse form data client
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public'))); // configure express to use public folder
app.use(session({ secret: 'krunal', resave: false, saveUninitialized: true, }));
// use express-flash
app.use(flash());
app.use('/:pageCalled',function(req, res, next){
if(req.params.pageCalled !== 'login' && !req.session.usuario){
console.log('Session usuario '+req.session.usuario);
res.redirect('/login');
} else {
next();
}
});
// routes
app.use('/', indexRouter);
app.use('/facturas', facturasRouter);
app.use('/dashboard', dashboardRouter);
app.use('/rendiciones', rendicionesRouter);
app.use('/perfil', perfilRouter);
app.use('/login', loginRouter);
app.use('/logout', logoutRouter);
app.use('/register', registerRouter);
app.use('/usuarios', usuariosRouter);
app.use('/tipocomprobante', tipocomprobanteRouter);
app.use('/tipogasto', tipogastoRouter);
app.use('/moneda', monedaRouter);
// 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;
这是我的login.js
const dbConnection = require('../connection');
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('login',{
sidenav: true
});
});
router.post('/', function(req, res, next){
var data = [req.body.usuario, req.body.password];
var sql = 'SELECT * FROM users WHERE user_email = ? AND user_password = ?;';
dbConnection.query(sql, data, function(err, result, fields){
if(result.length){
console.log(result[0].user_email);
req.session.usuario = result[0].user_email;
console.log(req.session.usuario);
} else {
console.log('err');
}
});
res.redirect('/dashboard');
});
module.exports = router;
我添加了它,以便在调用任何路由以检查用户是否登录之前检查会话:
app.use('/:pageCalled',function(req, res, next){
if(req.params.pageCalled !== 'login' && !req.session.usuario){
console.log('Session usuario '+req.session.usuario);
res.redirect('/login');
} else {
next();
}
});
我尝试了不同的模块,但是我不太了解它们。我尝试过使用redis,但我想这对我来说还很复杂。我是NodeJS的新手,但除了javascript之外,我对其他一些语言也有一些了解。
感谢您的回答。