Notice
Recent Posts
Recent Comments
Link
«   2026/03   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Tomato Basil

5-5. DB 모듈화, 회원가입, 로그인, INSERT, DELETE, 리팩토링 | 6-1. 유효성 검사, userId, name, return, err, update, delete 본문

DataBase

5-5. DB 모듈화, 회원가입, 로그인, INSERT, DELETE, 리팩토링 | 6-1. 유효성 검사, userId, name, return, err, update, delete

salt pepper 2024. 5. 13. 23:27
// Get the client
const mysql = require('mysql2');

// Create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password : 'sumincho',
  database: 'Youtube'
});

// A simple SELECT query
connection.query(
  'SELECT * FROM `users`',
  function (err, results, fields) {

    var {id, email, name} = results[0];
    console.log(id); // results contains rows returned by server
    console.log(email);
    console.log(name);
  }
);

 

이런 식으로 디비 쿼리들을 담은 코드를 매번 적기에는 양이 많다.

따라서 connection을 모듈화 시켜본다.

 

module.exports = connection

 

이 라인을 맨 밑에 추가해준다.

 

// Get the client
const mysql = require('mysql2');

// Create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password : 'sumincho',
  database: 'Youtube'
});

module.exports = connection

 

 

 

 

 

6-1.

추후 추가 예정입니다.