본문 바로가기
Coding/Raspberry pi - node.js

REST API 예제

by hyun-am 2019. 11. 17.

REST API란

 

REpresentaional State Transfer의 줄임말이며

 

Client가 Server에게 요청하는 4가지 방식입니다.

4가지 Method = POST, GET, PUT, DELETE

4가지 연산 = INSERT, SELECT, UPDATE, DELETE

URL : http://x.x.x.x:00000/path1/filename.txt

URI : http://x.x.x.x:00000/products/smartphone

 

POST예제

 

post-server.js

 

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

var mydata = {
    name : "",
    age:0,
    addr:"",
    tel:""
};

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

app.post('/member',(req,res)=>{
    console.log("POST method로 데이터 수신...");
    console.log("이름 : " + req.body.name);
    console.log("나이 : " + req.body.age);
    console.log("주소 : " + req.body.addr);
    console.log("전화 : " + req.body.tel);
    res.send("서버에서 확실하게 받았다고 함");
});

app.listen(60001,()=>{
    console.log("PEER1:서버(60001)가동중...");
});

 

post-client.js

 

const request = require('request');
var peer2data = {
    neme:"Hyun",
    age : 24,
    addr :"JeJu",
    tel : "010-222-3333"}
    
request.post(
    {url:'http://IP주소:60001/member',
    form:pee2data,
    headers:{"content-type":"application/x-www-form-urlencoded"}
    },
    (error,responsembody) =>{
        if(!error && response.statusCode == 200){
            console.log(body)
        }
    }
};

 

Get방식 예제

 

get-server.js

 

const express = require('express');
const app = express();

var mydata = {
    name:"홍길동",
    age:27,
    addr:"수원",
    tel : "010-999-9999"
};
var cnt;

const getmember = (req,res)=>{
    console.log("Server:Get(%d) > 데이터보냄!",cnt++);
    res.send(mydata);
}

app.get('/member',getmember);

app.listen(60001,()=>{
    console.log("Peer1:server is activated on 60001..");
});

 

get-client.js

 

const express = require('request');
request.get(
    {url:'http://IP:60001/member',
    headers : {"content-type":"application/json"}
    },
    function(error,res,body){
        let data = JSON.parse(body);
        if(!error && res.statusCode==200){
            console.log('GET서버로부터 수신했음!');
            console.log("이름:" + data.name);
            console.log("나이:" + data.age);
            console.log("주소:" + data.addr);
            console.log("연락처:"+data.tel);
        }
    }
);

 

PUT 예제

 

put-server.js

 

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

var mydata = {
    name:"홍길동",
    age:27,
    addr:"수원",
    tel:"010-999-9999"
};

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

app.put('/member',function(req,res){
    console.log("PUT method로 데이터 수신...");
    console.log("이름 : " + req.body.name);
    console.log("나이 : " + req.body.age);
    console.log("주소 : " + req.body.addr);
    console.log("전화 : " + req.body.tel);
    res.send("서버에서 확실하게 받았다고 함");
});

app.listen(60001,()=>{
    console.log("Peer1:서버(60001)가동중...");
});

 

put-client.js

 

const request = require('request');
var peer2data = {
    name:"hyun",
    age:25,
    addr:"성남",
    tel : "010-222-3333"}

request.put(
   {url:'http://IP주소:60001/member',
   form : peer2data,
   header:{"content-type":"application/x-www-form-urlencoded"}
   },
   function(error,response,body){
       if(!error && response.statusCode==200){
           console.log(body)
        }
    }
);
   

 

Delete 방식 예제

 

delete-server.js

 

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

var mydata = {
    name : "hyun",
    age : 25,
    addr : "jeju",
    tel : "010-222-3333"
};

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

app.delete('/member',function(req,res){
    console.log("DELETE method로 데이터 수신...");
    console.log("이름 : " + req.body.name + " 인 자료를 삭제합니다");
    res.send("서버에서 확실하게 받았다고 함");
});

app.listen(60001,()=>{
    console.log('Peer:서버(60001)가동중...');
});

 

delete-client.js

 

const request = require('request');
var peer2data = {
    name:"hyun",
    age:25,
    addr:"jeju",
    tel:"010-222-3333"}
    
request.delete(
    {url:'http://IP주소:60001/member',
    form:peer2data,
    headers:{"content-type":"application/x-www-form-urlencoded"}
    },
    function(error,response,body){
        if(!error && response.statusCode==200){
            console.log(body)
        }
    }
);

 

댓글