POST

POST방식으로 데이터를 전송할 때는 'body-parser'라는 미들웨어를 사용한다. 일종의 플러그인이라고 생각하면 된다.

사용자의 요청이 들어오면 일단 bodyParser 라는 미들웨어가 먼저 동작한다.

  1. bodyParser 미들웨어 설치

  2. bodyParser 가져온다

var bodyParser = require('body-parser');

3. bodyParser와 어플을 연결한다.

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

4.POST방식으로 라우팅이 동작한다! bodyParser로 인해 익명함수의 첫번째 인자인 req의 body 객체를 얻는다. 그리고 body 객체의 속성인 'title'과 'desc'를 얻을 수 있게 된다. (사용자가 전송한 데이터 중'title'을 body 객체의 'title' 라는 프로퍼티를 만들고 여기에에 값을 넣고, 'desc'는 body 객체의 'desc' 프로퍼티에 넣는다.)

app.post('/form_receiver',(req,res)=>{
    var title = req.body.title;
    var desc =  req.body.desc;
    res.send('Hello POST');
    res.send(title+','+desc);
});

Last updated

Was this helpful?