Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
1
Indexable
Never
const http = require('http');
const { Pool } = require('pg');
require('dotenv').config();

let hostname = 'localhost';
let port = 8000;

let server = http.createServer((request, response) => {
  response.statusCode = 200;
  response.setHeader('Content-Type', 'text/plain');
  response.end('');
});

const POOL = new Pool({
  user: process.env.PGUSER,
  host: process.env.PGHOST,
  database: process.env.PGDATABASE,
  password: process.env.PGPASSWORD,
  port: process.env.PGPORT,
});

let connectDb = async () => {
  try {
    await POOL.connect();
    let response = await POOL.query('SELECT * FROM context');
    console.log(response);

    await POOL.end();
  } catch (error) {
    console.log(error);
  }
};

connectDb(); // Вызываем функцию подключения к БД сразу после инициализации объекта POOL

module.exports = POOL; // Экспортируем объект POOL для использования в других частях приложения

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});