CON.CONNECT不是一个功能:使用KNEX.JS,Express-Session,PG和Connect-PG-Simple

发布于 2025-02-02 04:50:23 字数 2932 浏览 1 评论 0原文

我正在使用Express-Session,Connect-PG-Simple,Passport.js,Knex.js,PG和PostgreSQL进行网站,并且当前正在启动我的Session Store或Database Connection在我启动服务器时的问题。

我可以启动我的服务器,但也可以在控制台中获得此消息:

Failed to prune sessions: con.connect is not a function

我不知道这意味着什么,因为我没有“ con.connect”在我编写的代码中的任何位置。我找不到有关此在线的任何解释或解决方案,但我认为它必须与我的数据库和会话存储之间的连接有关。

这就是我的knexfile的样子:

const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '.env.development') });

const dbMode = 
   process.env.VITE_ENV === 'development' ? {
    client: "pg",
    connection: {
      host: 'localhost',
      port: 5432,
      user: process.env.VITE_DB_USER,
      password: process.env.VITE_DB_PASS,
      database: process.env.VITE_DB_NAME,
      charset: 'utf8'
    },
    migrations: {
      directory: './server/db/migrations',
      tableName: "knex_migrations"
    },
    seeds: {
      directory: './server/db/seeds'
    }
  } : {
    client: "pg",
    connection: process.env.DATABASE_URL,
    ssl: { require: true }
}

module.exports = dbMode;

这是我的会话文件:

const path = require('path');
const dbCon = require('../../knexfile');
require('dotenv').config({ path: path.join(__dirname, '..', '..', '.env.development') });
const express_session = require('express-session');
const pgSession = require('connect-pg-simple')(express_session);

const theSecret = process.env.VITE_SESSION_SECRET;

const session = express_session({
    store: new pgSession({ tableName: 'sessions', conObject: dbCon }),
    secret: theSecret,
    resave: false,
    saveUninitialized: false,
    cookie: { maxAge: 1000 * 60 * 60 * 24 },
})

module.exports = session;

我的db文件:

const knex = require('knex');
const dbConfig = require('../../knexfile');
const db = knex(dbConfig);

module.exports = db;

最后我的initserver.js文件:

const express = require('express');
const path = require('path');
const PORT = process.env.PORT || 5000;
const app = express();
const cors = require('cors');
const session = require('./db/session');
require('dotenv').config({ path: path.join(__dirname, '..', '.env') });
const { passport } = require('./passport');

app.use(cors({
    origin: process.env.VITE_CORS_ORIGIN,
    credentials: true
}));

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(session);

app.use(passport.initialize());
app.use(passport.session());

app.use(require('./routes'));

app.use(function(req, res, next) {
    res.status(404).send("Unable to find requested resource.")
});

app.use((err, req, res, next) => {
    if (err) {
        req.logout();
        next();
    }
    res.status(err.status || 500).send(err.message);
});

app.listen(PORT, () => {
    console.log(`Listening on port ${PORT}`)
});

没有适当的错误消息,所以我真的不知道是什么导致了我的会话商店或连接失败。

我真的很感谢您解决此问题的帮助,该问题似乎在网上没有太多信息。

I am making a site with express-session, connect-pg-simple, passport.js, Knex.js, pg and PostgreSQL and am currently experiencing what appears to be an issue with my session store or database connection when I start my server.

I can start my server but I also get this message in my console:

Failed to prune sessions: con.connect is not a function

I have no idea what this means as I don't have 'con.connect' anywhere in the code that I have written. I can't find any explanation or solution for this online, but I presume that it must be related to the connection between my database and the session store.

This is what my knexfile looks like:

const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '.env.development') });

const dbMode = 
   process.env.VITE_ENV === 'development' ? {
    client: "pg",
    connection: {
      host: 'localhost',
      port: 5432,
      user: process.env.VITE_DB_USER,
      password: process.env.VITE_DB_PASS,
      database: process.env.VITE_DB_NAME,
      charset: 'utf8'
    },
    migrations: {
      directory: './server/db/migrations',
      tableName: "knex_migrations"
    },
    seeds: {
      directory: './server/db/seeds'
    }
  } : {
    client: "pg",
    connection: process.env.DATABASE_URL,
    ssl: { require: true }
}

module.exports = dbMode;

Here's my session file:

const path = require('path');
const dbCon = require('../../knexfile');
require('dotenv').config({ path: path.join(__dirname, '..', '..', '.env.development') });
const express_session = require('express-session');
const pgSession = require('connect-pg-simple')(express_session);

const theSecret = process.env.VITE_SESSION_SECRET;

const session = express_session({
    store: new pgSession({ tableName: 'sessions', conObject: dbCon }),
    secret: theSecret,
    resave: false,
    saveUninitialized: false,
    cookie: { maxAge: 1000 * 60 * 60 * 24 },
})

module.exports = session;

My db file:

const knex = require('knex');
const dbConfig = require('../../knexfile');
const db = knex(dbConfig);

module.exports = db;

And finally my initServer.js file:

const express = require('express');
const path = require('path');
const PORT = process.env.PORT || 5000;
const app = express();
const cors = require('cors');
const session = require('./db/session');
require('dotenv').config({ path: path.join(__dirname, '..', '.env') });
const { passport } = require('./passport');

app.use(cors({
    origin: process.env.VITE_CORS_ORIGIN,
    credentials: true
}));

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(session);

app.use(passport.initialize());
app.use(passport.session());

app.use(require('./routes'));

app.use(function(req, res, next) {
    res.status(404).send("Unable to find requested resource.")
});

app.use((err, req, res, next) => {
    if (err) {
        req.logout();
        next();
    }
    res.status(err.status || 500).send(err.message);
});

app.listen(PORT, () => {
    console.log(`Listening on port ${PORT}`)
});

There is no proper error message for this so I really don't know what's causing my session store or the connection to fail.

I would really appreciate your help with solving this issue, which doesn't seem to have much info online.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝礼 2025-02-09 04:50:23

我不完全确定问题的确切原因是什么,但是它一定与Knex.js和Connect-PG-Simple之间的某种互动有关。

通过卸载Connect-PG简单,并用更适合KNEX(即Connect-Session-knex)的替代方案代替它,我就可以让会话商店启动而没有任何错误。

I'm not entirely sure what the precise cause of the problem was, but it must have been related to some sort of interaction between Knex.js and connect-pg-simple.

By uninstalling connect-pg-simple and replacing it with an alternative suited more specifically to Knex (namely connect-session-knex) I was able to get the session store to start up without any errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文