kkedory 2022. 4. 7. 00:01
728x90

board.sql

CREATE DATABASE db_test;

CREATE TABLE BOARD(
    ID VARCHAR2(50),
    TITLE VARCHAR2(200),
    CONTENT VARCHAR2(1000),
    WDATE DATE
);

 

$ npm install mysql

 

 

config/database.js

var mysql = require('mysql');
var db_info = {
    host: 'localhost',
    port: '3306',
    user: 'user_name',
    password: 'password',
    database: 'db_name'
}

module.exports = {
    init: function () {
        return mysql.createConnection(db_info);
    },
    connect: function(conn) {
        conn.connect(function(err) {
            if(err) console.error('mysql connection error : ' + err);
            else console.log('mysql is connected successfully!');
        });
    }
}

 

 

app.js

var express = require('express');
var app = express();
var db_config = require(__dirname + '/config/database.js');
var conn = db_config.init();
var bodyParser = require('body-parser');

db_config.connect(conn);

app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : false}));

app.get('/', function (req, res) {
    res.send('ROOT');
});

app.get('/list', function (req, res) {
    var sql = 'SELECT * FROM BOARD';    
    conn.query(sql, function (err, rows, fields) {
        if(err) console.log('query is not excuted. select fail...\n' + err);
        else res.render('list.ejs', {list : rows});
    });
});

app.get('/write', function (req, res) {
    res.render('write.ejs');
});

app.post('/writeAf', function (req, res) {
    var body = req.body;
    console.log(body);

    var sql = 'INSERT INTO BOARD VALUES(?, ?, ?, NOW())';
    var params = [body.id, body.title, body.content];
    console.log(sql);
    conn.query(sql, params, function(err) {
        if(err) console.log('query is not excuted. insert fail...\n' + err);
        else res.redirect('/list');
    });
});

app.listen(3000, () => console.log('Server is running on port 3000...'));

 

views/list.ejs

<html>
    <head>
        <meta charset="utf-8">
        <title>게시글</title>
    </head>
    <body>
        <h2>게시글 리스트</h2>
        <table border='1'>
            <colgroup>
                <col width='60'><col width='200'><col width='100'>
            </colgroup>
            <thead>
                <tr>
                    <th>글번호</th>
                    <th>글제목</th>
                    <th>작성자</th>
                </tr>
            </thead>
            <tbody>
                <% for(i = 0; i < list.length; i++) { %>
                <tr>
                    <td><%=i+1 %></td>
                    <td><%=list[i].TITLE %></td>
                    <td><%=list[i].ID %></td>
                </tr>
                <% } %>
            </tbody>
        </table boader='1'>
        
        <button onclick="location.href='../write'">글쓰기</button>
    </body>
</html>

 

views/write.ejs

<html>
    <head>
        <meta charset="utf-8">
        <title>글쓰기</title>
    </head>
    <body>
        <h2>게시글 작성</h2>
        <form action='/writeAf' method='post'>
            작성자 : <input type='text' name='id'><br>
            제목 : <input type='text' name='title'><br>
            내용 : <textarea name='content'></textarea><br>
            <button type='submit'>글쓰기</button>
        </form>
    </body>
</html>

 

제 서버를 작동시켜 쿼리문이 제대로 실행되는지 확인해보자.

 

게시글 작성폼

작성된 글 리스트가 제대로 조회됨을 확인

728x90