생활코딩66 [Nodejs-mysql] 테이블 만들고 모듈 분리하기 [생활코딩] 저자 목록 보기 기능 구현 main.js var author = require('./lib/author'); else if(pathname === '/author'){ author.home(request,response); } lib/author.js var db = require('./db'); var template = require('./template.js'); exports.home = function(request, response){ db.query('SELECT*FROM topic', function(error, topics){ db.query('SELECT*FROM author', function(error2, authors){ // author데이터 불러와 authors에 .. 2020. 8. 7. [Nodejs-mysql] DB 관련 코드 모듈로 분리하기 [생활코딩] Node.js 코드의 정리정돈 (topic) home 분리하기 main.js var topic = require('./lib/topic'); // topic모듈 불러오기 if(pathname === '/'){ if(queryData.id === undefined){ topic.home(request,response); // topic모듈의 home 사용하기 // 인자로 request,response 전달 } else { ... } } lib/topic.js var db = require('./db'); var template = require('./template.js'); exports.home = function(request,response){ // 한 모듈에서 여러개 내보낼 때는 ex.. 2020. 8. 7. [Nodejs-mysql] Select값 수정하기 [생활코딩] MySQL join을 이용해서 글수정 구현 수정페이지에 Select 넣고 선택되었던 option 출력하기 main.js if(pathname === '/update'){ db.query('SELECT*FROM topic', function(error, topics){ if(error){ throw error; } db.query('SELECT*FROM topic WHERE id=?', [queryData.id], function(error2, topic){ if(error2){ throw error2; } db.query('SELECT*FROM author', function(error3, authors){ // author값 받아서 authors에 담기 var list = template... 2020. 8. 7. [Nodejs-mysql] 데이터 Select의 option으로 출력하고 선택값 저장 [생활코딩] MySQL join을 이용해서 글생성 구현 데이터 Select의 option으로 만들기 if(pathname === '/create'){ db.query('SELECT*FROM topic', function(error, topics){ db.query('SELECT*FROM author', function(error2, authors){ // author값 가져와서 authors에 담기 var tag = ''; var i = 0; while(i < authors.length){ tag = tag + `${authors[i].name}` i++; } // authors값 반복문으로 option 만들어 tag에 담기 // value값으로 authors[i].id 저장 var title = 'WE.. 2020. 8. 6. [Nodejs-mysql] Join된 테이블에서 값 가져오기 [생활코딩] MySQL join을 이용해서 상세보기 구현 db.query('SELECT*FROM topic', function(error, topics){ if(error){ throw error;} db.query('SELECT*FROM topic LEFT JOIN author ON topic.author_id = author.id WHERE topic.id=?', [queryData.id], function(error2, topic){ // JOIN LEFT로 topic과 author 데이터 가져오기 // ON으로 두 테이블 매칭시키기 // WHERE에서 어떤 테이블의 id값 가져올지 명시하기 if(error2){throw error2;} var title = topic[0].title; var de.. 2020. 8. 6. [Nodejs-mysql] 데이터 수정/삭제하기 [생활코딩] MySQL로 글 수정 기능 구현 [생활코딩] MySQL로 글 삭제 기능 구현 수정할 데이터 폼에 출력 if(pathname === '/update'){ db.query('SELECT*FROM topic', function(error, topics){ // topic 데이터 불러와 topics에 담음 if(error){ throw error; } // 예외처리 db.query('SELECT*FROM topic WHERE id=?', [queryData.id], function(error2, topic){ // topic의 특정 id값을 가져와 topic에 담음 // id값은 배열로 가져와 ?에 치환하며, 그 값은 queryData.id if(error2){ throw error2; } // 예.. 2020. 8. 6. 이전 1 ··· 4 5 6 7 8 9 10 11 다음 반응형