Print
카테고리: [ Development ]
조회수: 242

1. 배경


2. js 파일 내 기본 설정

const { Pool } = require('pg')
const Query = require('pg').Query

const pool = new Pool({
  user: 'malladm',
  host: '[DB주소]',
  database: 'malldb',
  password: 'mall00',
  port: 5432,
})

3. 쿼리문

const read_sql = 'SELECT service, data_name, count(*), min(currenttime), max(currenttime) FROM api_log GROUP BY service, api';

exports.apicalls = function (req, res, next) {
  const query = new Query(read_sql)
  pool.query(query)

  var rows = [];

  query.on("row",row=>{
    rows.push(row);
  });
  query.on('end', () => {
    console.log(rows);
    console.log('query done')
    res.send(rows);
    res.status(200).end();
  });
  query.on('error', err => {
    console.error(err.stack)
  });
};