node ch.11 _ query 구문 개선
-
SQL Escape 구글링 해서 쿼리구문을 개선하는 데 활용할 수 있다.
기존 insert 문을 수정 /router/join/index.js
var express = require('express') var app = express() var router = express.Router() var path = require('path') var mysql = require('mysql') // DATABASE SETTING (Google Cloud SQL) var connection = mysql.createConnection({ host : '35.189.176.97', port : 3306, user : 'root', password : 'root', database : 'jsman' }); connection.connect(); router.get('/', function(req,res){ res.sendFile(path.join(__dirname, '../../public/join.html')) }) router.post('/', function(req,res){ var body = req.body; var email = body.email; var name = body.name; var passwd = body.password; var sql = {email : email, name : name, password : passwd }; var query = connection.query('insert into user set ?', sql, function(err, rows) { if(err) { throw err;} console.log("Data inserted!"); }) }) module.exports = router;
-
DB 로 입력된 결과를 클라이언트에 나타내기 (ejs)
/views/welcome.ejs 파일 생성
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>email ejs template</title> </head> <body> <h1>Welcom !! <%= name %> </h1> <p><%= id %> 번째 가입자 입니다</p> </body> </html>
-
/router/join/index.js 수정
var express = require('express') var app = express() var router = express.Router() var path = require('path') var mysql = require('mysql') // DATABASE SETTING (Google Cloud SQL) var connection = mysql.createConnection({ host : '35.189.176.97', port : 3306, user : 'root', password : 'root', database : 'jsman' }); connection.connect(); router.get('/', function(req,res){ res.sendFile(path.join(__dirname, '../../public/join.html')) }) router.post('/', function(req,res){ var body = req.body; var email = body.email; var name = body.name; var passwd = body.password; var sql = {email : email, name : name, password : passwd }; var query = connection.query('insert into user set ?', sql, function(err, rows) { if(err) { throw err;} res.render('welcome.ejs', {'name' : name, 'id': rows.insertId}) }) }) module.exports = router;
-
결과 확인