服务器在找到会话代码时挂起。我正在使用 Sequelize 来存储会话
Servers Hangs when finds session code. I'm using Sequelize to store the session
我有一个已经在运行的应用程序。当我对它的会话部分进行编码时,问题就开始了。当在控制器中找到这行代码时服务器挂起:
req.session.save(() => {
req.session.loggedIn = true;
});
用户-routes.js控制器是:
const router = require('express').Router();
console.log(__filename);
router.get('/login', (req, res) => {
res.render('login');
});
router.post('/verify', (req, res) => {
try {
// HERE *********************************************************************
req.session.save(() => {
req.session.loggedIn = true;
});
const messages = {};
res.render('dashboard', {messages, loggedIn:req.session.loggedIn},);
} catch (e) {
}
});
router.get('/newUser', (req, res) => {
res.render('newUser');
});
router.post('/saveUser', (req, res) => {
res.render('dashboard');
});
module.exports = router;
我的 server.js 是:
const root = require('app-root-path')
const express = require('express');
const path = require('path');
const exphbs = require('express-handlebars');
const session = require('express-session');
const SequelizeStore = require('connect-session-sequelize')(session.Store);
const sequelize = require('./config/connection');
const app = express();
const PORT = process.env.PORT || 3000;
const routes = require('./controllers/index');
const configSession = {
secret: 'this is a secret',
cookie: {maxAge: 86400,},
resave: false,
saveUninitialized: true,
store: new SequelizeStore({db: sequelize}),
};
app.use(express.json());
app.use(express.static(root + path.sep + 'public'));
app.use(express.urlencoded({extended: true}));
app.use(routes);
app.use(session(configSession));
const hbs = exphbs.create();
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
const run = async () => {
//const sequelizeResult = await sequelize.sync({force: true});
//console.log("---> sequelizeResult :" + (sequelizeResult));
await app.listen(PORT, () => {
console.log('Server listening on port %j', PORT);
});
}
//RUN
run();
我正在使用 MySQL。我停止了服务器,删除了数据库表,重新创建并为它们播种。
可能是什么问题?非常感谢您的帮助。
Sequelize 需要在服务器启动时同步 tables(并添加会话 table)。我正在取消注释 sequelize.sync() 被调用的行
const run = async () => {
const sequelizeResult = await sequelize.sync({force: true});
//console.log("---> sequelizeResult :" + (sequelizeResult));
await app.listen(PORT, () => {
console.log('Server listening on port %j', PORT);
});
}
问题解决了。
我有一个已经在运行的应用程序。当我对它的会话部分进行编码时,问题就开始了。当在控制器中找到这行代码时服务器挂起:
req.session.save(() => {
req.session.loggedIn = true;
});
用户-routes.js控制器是:
const router = require('express').Router();
console.log(__filename);
router.get('/login', (req, res) => {
res.render('login');
});
router.post('/verify', (req, res) => {
try {
// HERE *********************************************************************
req.session.save(() => {
req.session.loggedIn = true;
});
const messages = {};
res.render('dashboard', {messages, loggedIn:req.session.loggedIn},);
} catch (e) {
}
});
router.get('/newUser', (req, res) => {
res.render('newUser');
});
router.post('/saveUser', (req, res) => {
res.render('dashboard');
});
module.exports = router;
我的 server.js 是:
const root = require('app-root-path')
const express = require('express');
const path = require('path');
const exphbs = require('express-handlebars');
const session = require('express-session');
const SequelizeStore = require('connect-session-sequelize')(session.Store);
const sequelize = require('./config/connection');
const app = express();
const PORT = process.env.PORT || 3000;
const routes = require('./controllers/index');
const configSession = {
secret: 'this is a secret',
cookie: {maxAge: 86400,},
resave: false,
saveUninitialized: true,
store: new SequelizeStore({db: sequelize}),
};
app.use(express.json());
app.use(express.static(root + path.sep + 'public'));
app.use(express.urlencoded({extended: true}));
app.use(routes);
app.use(session(configSession));
const hbs = exphbs.create();
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
const run = async () => {
//const sequelizeResult = await sequelize.sync({force: true});
//console.log("---> sequelizeResult :" + (sequelizeResult));
await app.listen(PORT, () => {
console.log('Server listening on port %j', PORT);
});
}
//RUN
run();
我正在使用 MySQL。我停止了服务器,删除了数据库表,重新创建并为它们播种。
可能是什么问题?非常感谢您的帮助。
Sequelize 需要在服务器启动时同步 tables(并添加会话 table)。我正在取消注释 sequelize.sync() 被调用的行
const run = async () => {
const sequelizeResult = await sequelize.sync({force: true});
//console.log("---> sequelizeResult :" + (sequelizeResult));
await app.listen(PORT, () => {
console.log('Server listening on port %j', PORT);
});
}
问题解决了。