상세 컨텐츠

본문 제목

nodejs 및 jQuery 를 이용한 서버와의 통신 참고하기

IT공부방/jQuery, ajax, java

by 동해둘리 2016. 8. 5. 17:28

본문

반응형






향후, 참고를 위해 좋은 자료가 있는 포스팅을 기록으로 남김니다.


자료출처 : http://cafe.naver.com/jjdev/1714



[step1] mysql database:ksmart

 

member table

 

 

[step2] nodejs server 코드

 

  1. // mysql dbms연결
  2. var mysql = require('mysql');
  3. var client = mysql.createConnection({
  4.     user:'root',
  5.     password:'java1234',
  6.     database:'ksmart'
  7. });
  8. var http = require('http');
  9. var express = require('express');
  10. var app = express();
  11. app.use(express.static('public'));
  12. app.use(express.bodyParser());
  13. app.use(app.router);
  14. // 멤버 목록
  15. app.all('/list',function(request,response){
  16.     var sql = 'select * from member';
  17.     client.query(sql,function(error,result){       
  18.     //  console.log(result);
  19.         response.send(result)
  20.     });
  21. });
  22. // 멤버 추가
  23. app.all('/add',function(request,response){
  24.     var id = request.param('id');
  25.     var pw = request.param('pw');
  26.     var name = request.param('name');
  27.     var age = request.param('age');
  28.     var gender = request.param('gender');
  29.    
  30.     var sql = "insert into member(id,pw,name,age,gender) values(?,?,?,?,?)";
  31.     client.query(sql,
  32.                 [id,pw,name,age,gender],
  33.                 function(error,result){
  34.                     response.send(id);
  35.     });
  36. });
  37. // 멤버 삭제
  38. app.all('/remove',function(request,response){
  39.     var ck = request.param('ck'); // 배열
  40.     for(var i=0; i<ck.length;i++){
  41.         var sql = 'delete from member where id=?';
  42.         client.query(sql,[ck[i]],function(){});
  43.     }
  44.     response.send(ck);
  45. });
  46. // 멤버 수정
  47. app.all('/modifyById',function(request,response){
  48.     var id = request.param('id');
  49.     var pw = request.param('pw');
  50.     var name = request.param('name');
  51.     var age = request.param('age');
  52.     var gender = request.param('gender');
  53.    
  54.     var sql = "update member set pw=?,name=?,age=?,gender=? where id=?";
  55.     client.query(sql,
  56.                 [pw,name,age,gender,id],
  57.                 function(error,result){
  58.                     response.send(id);
  59.     });
  60. });
  61.  
  62. http.createServer(app).listen(80,function(){
  63.     console.log('Server Running at http://127.0.0.1');
  64. });

 

[step3] client 코드

 

  1. <!DOCTYPE>
  2. <html>
  3. <head>
  4. <script>
  5. $(document).ready(function(){
  6.     // 삭제
  7.     $('#btn_remove').on('click',function(){
  8.         var ck = new Array();
  9.         $('.ck:checked').each(function(index,item){
  10.             ck.push($(item).val());
  11.         });
  12.         $.ajax({
  13.             url:'/remove',
  14.             type:'GET',
  15.             data:{ck:ck},
  16.             success:function(data){
  17.                 alert(data+'삭제');
  18.                 $('#btn_list').trigger('click');
  19.             }
  20.         });
  21.     });
  22.  
  23.     // 수정
  24.     $('#btn_add').on('click',function(){
  25.         var id = $('#id').val();
  26.         var pw = $('#pw').val();
  27.         var name = $('#name').val();
  28.         var age = $('#age').val();
  29.         var gender = $('#gender').val();
  30.  
  31.         $.ajax({
  32.             url:'/add',
  33.             type:'POST',
  34.             data:{id:id,pw:pw,name:name,age:age,gender:gender},
  35.             success:function(data){
  36.                 alert(data+'님 추가');
  37.                 $('#btn_list').trigger('click');
  38.             }
  39.         });
  40.     });
  41.     // 추가
  42.     $('#btn_modify').on('click',function(){
  43.         var id = $('#id').val();
  44.         var pw = $('#pw').val();
  45.         var name = $('#name').val();
  46.         var age = $('#age').val();
  47.         var gender = $('#gender').val();
  48.  
  49.         $.ajax({
  50.             url:'/modifyById',
  51.             type:'POST',
  52.             data:{id:id,pw:pw,name:name,age:age,gender:gender},
  53.             success:function(data){
  54.                 alert(data+'님 수정');
  55.                 $('#btn_list').trigger('click');
  56.             }
  57.         });
  58.     });
  59.     // 목록
  60.     $('#btn_list').on('click',function(){
  61.         $.ajax({
  62.             url:'/list',
  63.             type:'GET',
  64.             success:function(data){
  65.                 $('#list').empty();
  66.                 $(data).each(function(index,item){
  67.                     $('#list').append('<tr>');
  68.                     $('#list').append('<td><input type="checkbox" class="ck" value="'+item.id+'"></td>');
  69.                     $('#list').append('<td>'+item.id+'</td>');
  70.                     $('#list').append('<td>'+item.pw+'</td>');
  71.                     $('#list').append('<td>'+item.name+'</td>');
  72.                     $('#list').append('<td>'+item.age+'</td>');
  73.                     $('#list').append('<td>'+item.gender+'</td>');
  74.                     $('#list').append('</tr>');
  75.                 });
  76.             }
  77.         });
  78.     });
  79. });
  80. </script>
  81. </head>
  82. <body>
  83.     <table border="1">
  84.         <thead>
  85.             <tr>
  86.                 <th>id</th>
  87.                 <th>pw</th>
  88.                 <th>name</th>
  89.                 <th>age</th>
  90.                 <th>gender</th>
  91.                 <th>추가</th>
  92.                 <th>수정</th>
  93.             </tr>
  94.         </thead>
  95.         <tbody>
  96.             <tr>
  97.                 <td><input type="text" id="id"></td>
  98.                 <td><input type="password" id="pw"></td>
  99.                 <td><input type="text" id="name"></td>
  100.                 <td><input type="text" id="age"></td>
  101.                 <td>
  102.                     <select id="gender">
  103.                         <option value="m"></option>
  104.                         <option value="f"></option>
  105.                     </select>
  106.                 </td>
  107.                 <td><button id="btn_add">add</button></td>
  108.                 <td><button id="btn_modify">modify</button></td>
  109.             </tr>
  110.         </tbody>
  111.     </table>
  112.  
  113.     <button id="btn_list">회원리스트</button>
  114.     <table border="1">
  115.         <thead>
  116.             <tr>
  117.                 <th></th>
  118.                 <th>id</th>
  119.                 <th>pw</th>
  120.                 <th>name</th>
  121.                 <th>age</th>
  122.                 <th>gender</th>
  123.             </tr>
  124.         </thead>
  125.         <tbody id="list">
  126.         </tbody>
  127.     </table>
  128.     <button id="btn_remove">remove</button>
  129. </body>
  130. </html>

 

[실행화면]

 

 

 


반응형

관련글 더보기

댓글 영역