node.js - 1
2019.10.10 13:02
Express에서 MySql 사용
조회 수 754 추천 수 0 댓글 0
프로젝트 폴더 생성 및 초기화
$ mkdir express-mysql $ cd express-mysql $ npm init --yes
필요한 NPM 설치
$ npm install express mysql --save --save-exact
package.json 수정
{ "name": "express-mysql-example", "version": "0.0.1", "description": "", "main": "index.js", "scripts": { "start": "node index" }, "dependencies": { "express": "4.14.0", "mysql": "2.11.1" } }
루트 디렉터리에 index.js를 생성
console.log('Hello world')
애플리케이션을 실행
$ npm start
테이블 생성 및 테스트용 데이터 삽입
CREATE DATABASE my_db; USE my_db; CREATE TABLE Persons ( id int, name varchar(255), age int ); INSERT INTO Persons (id, name, age) VALUES (1, 'John Doe', 20); SELECT * FROM Persons;
Node.js와 MySQL 연동
var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : '< MySQL username >', password : '< MySQL password >', port : < port >, database : 'my_db' }); connection.connect(); connection.query('SELECT * from Persons', function(err, rows, fields) { if (!err) console.log('The solution is: ', rows); else console.log('Error while performing Query.', err); }); connection.end();
The solution is: [ RowDataPacket { id: 1, name: 'John Doe', age: 20 } ]
Route 작성
index.js를 아래와 같이 변경한다.
var express = require('express'); var mysql = require('mysql'); var dbconfig = require('./config/database.js'); var connection = mysql.createConnection(dbconfig); var app = express(); // configuration =============================================================== app.set('port', process.env.PORT || 3000); app.get('/', function(req, res){ res.send('Root'); }); app.get('/persons', function(req, res){ connection.query('SELECT * from Persons', function(err, rows) { if(err) throw err; console.log('The solution is: ', rows); res.send(rows); }); }); app.listen(app.get('port'), function () { console.log('Express server listening on port ' + app.get('port')); });
루트 디렉터리 아래 config 디렉터리에 저장
// config/database.js module.exports = { host : 'localhost', user : '< MySQL username >', password : '< MySQL password >', port : < port >, database : 'my_db' };
서버를 실행한다.
$ npm start
localhost:3000/persons에 접속하여 결과를 확인한다.
번호 | 분류 | 제목 | 날짜 | 조회 수 |
---|---|---|---|---|
38 | node.js - 1 | Express에서 POST 방식 사용하기 | 2019.10.16 | 319 |
» | node.js - 1 | Express에서 MySql 사용 | 2019.10.10 | 754 |
36 | node.js - 2 | Node.js MySQL Limit | 2019.10.10 | 247 |
35 | node.js - 2 | Node.js MySQL Update | 2019.10.10 | 241 |
34 | node.js - 2 | Node.js MySQL Drop Table | 2019.10.08 | 303 |
33 | node.js - 2 | Node.js MySQL Delete | 2019.10.08 | 248 |
32 | node.js - 2 | Node.js MySQL Order By | 2019.10.08 | 253 |
31 | node.js - 2 | Node.js MySQL Where | 2019.10.08 | 451 |
30 | node.js - 2 | Node.js MySQL Select From | 2019.10.08 | 603 |
29 | node.js - 2 | Node.js MySQL Insert Into | 2019.10.01 | 844 |
28 | node.js - 2 | Node.js MySQL Create Table | 2019.10.01 | 287 |
27 | PUG | PUG 07 / INCLUDES | 2019.10.01 | 317 |
26 | PUG | PUG 06 / DOCTYPE | 2019.10.01 | 401 |
25 | node.js - 2 | Node.js MySQL | 2019.09.27 | 307 |
24 | node.js - 2 | NPM의 사용 | 2019.09.26 | 298 |
23 | node.js - 2 | Node.js URL Module | 2019.09.26 | 438 |
22 | PUG | PUG 05 / Conditionals | 2019.09.19 | 670 |
21 | PUG | PUG 04 / Comments | 2019.09.19 | 304 |
20 | PUG | PUG 03 / Code | 2019.09.19 | 677 |
19 | PUG | PUG 02 / Case | 2019.09.19 | 328 |