web_fullstack

Express + MySQL 연동

AI Coder 2020. 7. 31. 19:46

1. npm install express mysql  설치

 

2. 다음과 같이 코딩 한다. 

 

var mysql = require('mysql')

var connection = mysql.createConnection({

    host : 'localhost',

    user : 'root',

    password : 'hello1234',

    database : 'fsts'

})

connection.connect()

connection.query('SELECT * FROM staff'function(errrowsfields) {

    if (errthrow err;

    for(var i=0i<rows.lengthi++){

        console.log(rows[i].name + " , " + rows[i].id + " , " + rows[i].department + " , " + rows[i].location );

    }

})

connection.end();

 

staff table :

result: