Private network : ENOTFOUND postgres.railway.internal
pierro-mojito
PROOP

2 years ago

Hi everyone,

I face this issue using a NextJS application, specifically in a api handler.

import { Pool } from "pg";
import type { NextApiRequest, NextApiResponse } from "next";

const regex =
  /^[a-zA-Z0-9.!#$%&\'*+\\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/;

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  if (req.method !== "POST") {
    res.status(405).json({ message: "Only POST requests allowed" });
    return;
  }

  const { email, interest } = req.body;

  ...

  try {
    const pool = new Pool({
      user: process.env.DB_USER,
      host: process.env.DB_HOST,
      database: process.env.DB_NAME,
      password: process.env.DB_PASSWORD,
      port: Number(process.env.DB_PORT),
    });

    await pool.query(
      "CREATE TABLE IF NOT EXISTS email_list(email TEXT, interest TEXT)"
    );

    let result = await pool.query("SELECT 1 FROM email_list WHERE email = $1", [
      email,
    ]);
    if (result.rows.length > 0) {
      await pool.query("UPDATE email_list SET interest = $1 WHERE email = $2", [
        interest,
        email,
      ]);

      res.status(200).json({ message: "Interest updated" });
      return;
    }

    await pool.query("INSERT INTO email_list(email, interest) VALUES($1, $2)", [
      email,
      interest,
    ]);

    res.status(200).json({ message: "Email added" });
  } catch (err) {
    console.error(err);
    res.status(500).json({
      message:
        "Database error, please report this issue on our social networks",
    });
  }
}

I didn't add a sleep 3 in this endpoint because it take more than 3 seconds to fill the form and submit it.
The VPC dns should be up I guess, maybe I'm wrong ?

But I don't really want to wait 3 seconds every requests I do ^^

This error is known ?

Have a good day

2 Replies

pierro-mojito
PROOP

2 years ago

62a7d631-15d2-4bde-a633-b2e28b306105


pierro-mojito
PROOP

2 years ago

Here is my CMD statement in my Dockerfile btw :

CMD sleep 5 && node server.js

Loading...