작업 노트

Node.js + MySQL 데이터베이스를 통한 게시판 본문

javascript/w3schools nodejs

Node.js + MySQL 데이터베이스를 통한 게시판

달빛가면 2016. 10. 28. 15:55
2015.07.22 19:27

Node.js + MySQL 데이터베이스를 통한 게시판 구현

 

모던 웹을 위한 Node.js 프로그래밍 9장 전체 소스 입니다.

 

 ch09.zip

 

// 모듈을 추출합니다.
var fs = require('fs');
var http = require('http');

var ejs = require('ejs');    // npm install ejs
var mysql = require('mysql');   // npm install mysql
var express = require('express');  // npm install 
express@3.4.7
var bodyParser = require('body-parser');  // npm install body-parser

 

// 데이터베이스와 연결합니다.
var client = mysql.createConnection({
    user: 'root',
    password: '3243aa',
    database: 'Company'
});

 

// 서버를 생성합니다.
var app = express();
app.use(bodyParser());
app.use(app.router);

 

// 서버를 실행합니다.
http.createServer(app).listen(52273, function () {
    console.log('server running at 
http://127.0.0.1:52273');
});

 

// 라우트를 수행합니다.
app.get('/', function (request, response) { 
    // 파일을 읽습니다.
    fs.readFile('list.html', 'utf8', function (error, data) {
        // 데이터베이스 쿼리를 실행합니다.
        client.query('SELECT * FROM products', function (error, results) {
            // 응답합니다.
            response.send(ejs.render(data, {
                data: results
            }));
        });
    });
});

 

app.get('/delete/:id'function (request, response) { 
    // 데이터베이스 쿼리를 실행합니다.
    client.query('DELETE FROM products WHERE id=?', [request.param('id')], function () {
        // 응답합니다.
        response.redirect('/');
    });
});

 

app.get('/insert'function (request, response) { 
    // 파일을 읽습니다.
    fs.readFile('insert.html', 'utf8', function (error, data) {
        // 응답합니다.
        response.send(data);
    });
});

 

app.post('/insert', function (request, response) {
    // 변수를 선언합니다.
    var body = request.body;

    // 데이터베이스 쿼리를 실행합니다.
    client.query('INSERT INTO products (name, modelnumber, series) VALUES (?, ?, ?)', [
        body.name, body.modelnumber, body.series
    ], function () {
        // 응답합니다.
        response.redirect('/');
    });
});

 

app.get('/edit/:id'function (request, response) {
     // 파일을 읽습니다.
    fs.readFile('edit.html', 'utf8', function (error, data) {
        // 데이터베이스 쿼리를 실행합니다.
        client.query('SELECT * FROM products WHERE id = ?', [
            request.param('id')
        ], function (error, result) {
            // 응답합니다.
            response.send(ejs.render(data, {
                data: result[0]
            }));
        });
    });
});

 

app.post('/edit/:id'function (request, response) {
     // 변수를 선언합니다.
    var body = request.body

    // 데이터베이스 쿼리를 실행합니다.
    client.query('UPDATE products SET name=?, modelnumber=?, series=? WHERE id=?', [
        body.name, body.modelnumber, body.series, request.param('id')
    ], function () {
        // 응답합니다.
        response.redirect('/');
    });
});


'javascript > w3schools nodejs' 카테고리의 다른 글

게시판 예제  (0) 2017.02.23
[Node.js 강좌] 게시판 페이지 기능 추가  (0) 2016.10.28
Node.js 참고 사이트  (0) 2016.06.24
Node.js 이해  (0) 2016.06.24
Nodejs API  (0) 2016.06.24
Comments