0
Node.js에 mysql
모듈을 사용하고 있습니다. 내 모델 파일에서 현재 각 메서드에서 연결 상수를 지정하고 있습니다. 그러나 이것은 많은 공간을 차지하고 있으며 이상적이지 않다는 것을 알고 있습니다.Node.js에서 코드 반복을 피하기 위해 MySQL 연결을 넣을 곳
다음은 그 모습입니다.
doSomething:() => {
var connection = mysql.createConnection({
host : config.database.host,
database : config.database.database,
user : config.database.user,
password : config.database.password
});
connection.query(...);
connection.destroy();
},
doSomethingElse:() => {
var connection = mysql.createConnection({
host : config.database.host,
database : config.database.database,
user : config.database.user,
password : config.database.password
});
connection.query(...);
connection.destroy();
},
누구나 정리할 수있는 방법을 권장하고 중복 코드를 줄일 수 있습니까?
당신을 감사합니다! 그리고 연결을 어디에서 끝내겠습니까? – Joel
@Joel 내 편집보기 –