본문 바로가기

학습노트/데이터베이스

express와 MySQL 연동

const express = require('express');
const mysql = require('mysql2');
const app = express();
const port = 8080;


// mysql 계정 연동
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '****',
  database: 'boilerdb'
});

// mysql 연결
connection.connect(console.log('mysql is connected'));

// mysql로 query보내기
connection.query('SELECT * from users', (error, rows, fields) => {
  if(error) throw error;
  console.log('User INFO IS ', rows);
})

app.get('/', (req, res) => res.send("hello world!"));

app.listen(port, () => console.log(`server is running on port ${port}`));