본문 바로가기
Back_end/node.js

node.js) post()

by nomfang 2021. 4. 20.
728x90
반응형

get()에서의 정보 사용은 url에 직접 적용되면서 새 링크를 Create했다면

post()는 html에서 사용자에게 받은 정보를

html의 body에 저장하여 읽어올 수 있다 Read

 

url을 이용하는 get()은 page의 링크를 생성하는 용도

body를 이용하는 post()는 page의 정보를 읽어와서 사용한다

 

post()를 이용하는 이유는 모든 정보를 url에 담을 수 없기 때문..인것 같고

특히 사용자의 개인정보나 id password 등의 민감 정보들을 쉽게 접근하지 않으면서 사용해야 하기 때문인 것 같다

 

node.js에서 post()로 받은 정보는 숨겨져 있기 때문에

사용하기 위해서는 body-parser 패키지가 필요하다

 

npm install body-parser 로 설치

 

 

1-1.  express 객체에서 사용 가능하다

const express = require('express');

const app = express();
app.use(express.urlencoded());

 

 

1-2. body parser를 직접 불러와서 사용할 수도 있음

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

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

 

2. html 의 form 태그 method 속성을 post로 설정해준다

<form method="post" action="/">
        이름 : <input type="text" name="name"> <br />
        id : <input type="text" name="id"> <br />
        pw : <input type="text" name="pw"> <br />
        <input type="submit" value="button">
</form>

3. node js에서는 요청 객체 내의 body 객체로 받아서 사용 가능하다

app.post('/', (요청, 응답)=>{

    console.log(요청.body);
  
})
반응형

'Back_end > node.js' 카테고리의 다른 글

node.js) nunjucks (html에 값 전달)  (0) 2021.04.22
node.js ) mysql 연결 및 사용(maria DB)  (0) 2021.04.22
node.js) 환경변수 설정 (dotenv)  (0) 2021.04.21
node.js) 시작 - 서버 실행, get()  (0) 2021.04.20
node.js 란?  (0) 2021.04.19

댓글