nodejs的h12请求超时heroku服务器

时间:2021-03-09 09:01:02

标签: node.js heroku

我在 heroku 服务器上部署了 nodejs 代码。

我面临 H12 请求超时问题

enter image description here

这里是与 pgsql 的数据库连接,它是异步的

const { Pool } = require("pg");
import nodeEnvFile = require("node-env-file");
import { Query } from "src/models/query";
nodeEnvFile(".env");

const config = {
    user: process.env.DB_USER,
    host: process.env.DB_IP,
    database: process.env.DB,
    password: process.env.DB_PASSWORD,
    port: process.env.DB_PORT,
    max: 5, // max number of connection can be open to database
    idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
    ssl: true
};

const pool = new Pool(config);

export class DB {
    static async query(queryInfo: Query): Promise<any> {
        // to hold undefined values while stringfy json
        const replacer = (_key, value) => typeof value === "undefined" ? null : value;
        let _arguments = "()";
        if (queryInfo.arguments) {
            _arguments = `('${JSON.stringify(queryInfo.arguments, replacer)}'::json)`;
        }
        const query = `select * from ${queryInfo.name} ${_arguments} as info;`;
        console.log(query);
        try {
            return pool.query(query)
                .then((response) => {
                    console.log("response", response);
                    return response.rows[0].info;
                })
                .catch((err) => {
                    console.log("Db Error : ", err);
                    throw err;
                });
        } catch (err) {
            console.log("err", err);
            throw err;
        }
    }
}

这是我如何调用这个数据库

const _loginInfo: any = await AuthFacade.login(credentials);

和门面函数

static async login(credentials: LoginCredentials): Promise<{ userId: number, token: string }> {
    const query = new Query({
        name: this.PROCS.LOGIN,
        arguments: credentials
    });
    return DB.query(query);
}

相同的编码在本地工作,它也在 heroku 上工作。但突然间我遇到了这个问题。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

pgsql 的连接有问题。我使用的是 pg 的第 7 版,我猜这不适用于 HeroKu。于是我更新了pg的版本

"pg": "^8.5.1",

并更新池配置

const Pool = require("pg-pool");
import nodeEnvFile = require("node-env-file");
import { Query } from "src/models/query";
nodeEnvFile(".env");

const config = {
    user: process.env.DB_USER,
    host: process.env.DB_IP,
    database: process.env.DB,
    password: process.env.DB_PASSWORD,
    port: process.env.DB_PORT,
    max: 5, // max number of connection can be open to database
    idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
    ssl: { rejectUnauthorized: false }
};

const pool = new Pool(config);

export class DB {
    static async query(queryInfo: Query): Promise<any> {
        // to hold undefined values while stringfy json
        const replacer = (_key, value) => typeof value === "undefined" ? null : value;
        let _arguments = "()";
        if (queryInfo.arguments) {
            _arguments = `('${JSON.stringify(queryInfo.arguments, replacer)}'::json)`;
        }
        const query = `select * from ${queryInfo.name} ${_arguments} as info;`;
        console.log(query);
        try {
            return pool.query(query)
                .then((response) => {
                    return response.rows[0].info;
                })
                .catch((err) => {
                    console.log("Db Error : ", err);
                    throw err;
                });
        } catch (err) {
            console.log("err", err);
            throw err;
        }
    }
}

现在可以使用了