简体   繁体   English

在Vagrant中将Node.js连接到Apache服务器上的Postgres

[英]Connecting Node.js to postgres on apache server in vagrant

Hello there fellow coders. 您好,各位编码员。 I am rebuilding my entire backend for a web application and switching from a php backend to node.js using typescript. 我正在为Web应用程序重建整个后端,并使用Typescript从php后端切换到node.js。 I'm gonna reuse all my old sql files since my old database structure is sufficient. 由于我的旧数据库结构已足够,因此我将重用所有旧的sql文件。

I'm launching my node webapp via vagrant on an apache server using ansible scripts. 我正在使用ansible脚本通过apache服务器上的vagrant启动节点webapp。 Then I manually SSH into the VM and execute "npm start" to start my webapp. 然后,我手动通过SSH进入虚拟机并执行“ npm start”启动我的Web应用程序。 This should start my app at localhost:3000 and just show a simple "Hello world" text, just to confirm that the app is connected to postgres and running correctly. 这应该在localhost:3000上启动我的应用程序,并仅显示一个简单的“ Hello world”文本,以确认该应用程序已连接到postgres并正常运行。

Im really new to node and javascript/typescript so sorry in advance if I ask silly questions. 我真的是节点和javascript / typescript的新手,所以如果我问一些愚蠢的问题,请提前抱歉。

Instead of the expected behavior I get a "Failed to prune sessions: relation "session" does not exist. 而不是预期的行为,我得到了“无法修剪会话:关系”会话”不存在。

My connection to postgres from the node app looks like this: 我从节点应用程序到postgres的连接看起来像这样:

const app = express();
const session = require ("express-session");
const pg = require ("pg");

const Pool = require("pg-pool");



// Create a pool once per process and reuse it
const pgPool = new Pool ({
  host: localhost
  port: 5432,
  user: "postgres",
  password: "postgres",
  database: "test,
});

app.use(session({
  store: new(require("connect-pg-pool")(session))({
    pool: pgPool
  }),
  secret: SESSION_SECRET,
  cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 }, // 30 days
  resave: true,
  autoreconnect: true,
  saveUninitialized: true,

}));
app.set("port", 3000);
app.set("views", path.join(__dirname, "../views"));
app.set("view engine", "pug");
app.get("/", homeController.index);

And my server.ts file is where my app listens to the port as shown below: 我的server.ts文件是我的应用程序侦听端口的位置,如下所示:

const server = app.listen(app.get("port"), () => {
  console.log(
    "  App is running at http://localhost:%d in %s mode",
    app.get("port"),
    app.get("env")
  );
  console.log("  Press CTRL-C to stop\n");
});

run "psql < node_modules/connect-pg-simple/table.sql" in the project folder, this creates the session table which is used by express-session as the middleware. 在项目文件夹中运行“ psql <node_modules / connect-pg-simple / table.sql”,这将创建会话表,express-session将其用作中间件。 I had simply forgotten to do this. 我只是忘记这样做了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM