2017-02-13 6 views
0

설명서를 읽고 연결 풀링을 확인하려고합니다.올바르게 연결 풀링을하고 있습니까? node-postgres 라이브러리 사용

/* src/config/db.js */ 
const pg = require('pg'); 

const dbConfig = { 
    host: process.env.DB_HOST, 
    port: process.env.DB_PORT, 
    user: process.env.DB_USER, 
    password: process.env.DB_PASS, 
    database: process.env.DB_DATABASE, 
    max: 25, 
    idleTimeoutMillis: 5000 
}; 

const pool = new pg.Pool(dbConfig); 

module.exports = { 
    pool 
}; 

내가 경로와 컨트롤러의 무리가 있습니다

문서는 내가 pool를 작성하여 내보낼 config/db.js 파일을 만든 있도록 풀 오래 살았해야한다고 말한다. 예를 들어 컨트롤러에 몇 가지 메소드가있는 /markers 엔드 포인트가 있습니다. 컨트롤러 파일에서 config/db.js의 풀을 가져 와서 사용하고 있습니다. 그 확인은? 또한

const pool = require('../config/db').pool; 

const create = function (req, res, next) { 
    const data = { 
    created_by: req.body.createdBy, 
    title: req.body.title, 
    description: req.body.description, 
    lat: req.body.lat, 
    lng: req.body.lng 
    }; 

    pool.connect((err, client, done) => { 
    if (err) { 
     done(); 
     // console.log(err); 
     return res.status(500).json({ success: false, data: err }); 
    } 
    client.query(
     'INSERT INTO markers(created_by, title, description, lat, lng, geography)\ 
     values($1, $2, $3, $4::decimal, $5::decimal, ST_SetSRID(ST_MakePoint($5::decimal, $4::decimal), $6))', 
     [ 
     data.created_by, 
     data.title, 
     data.description, 
     data.lat, 
     data.lng, 
     4326 
     ], 
     function(err, res) { 
     done(); 
     if (err) { 
      // console.log(err); 
     } 
     } 
    ); 
    return res.status(200).json({ success: true }); 
    }); 
}; 

, 어떻게 삽입이 성공하면 알 수없는 오류가 존재하지 않는 경우는 그냥 200 성공을 반환하지 않도록 삽입이 성공했는지 확인합니까?

답변

1

맞습니다.

오류를 확인하는 경우 바로 찾을 수있는 콜백에서 if (err), 오류가 반환되지 않는 경우 삽입 성공을 의미합니다.

+0

고마워! 하지만 create 함수에서'res'에 접근 할 수 없기 때문에 어떻게 처리해야할까요? – user1354934