joonsei
Hello, world!
joonsei
전체 방문자
오늘
어제
  • 분류 전체보기 (32)
    • nodejs (7)
    • javascript (3)
    • Elasticsearch (11)
    • Electron (1)
    • oracle (1)
    • etc (7)
    • MongoDB (1)

인기 글

최근 댓글

최근 글

joonsei
nodejs

body-parser (urlencoded, extended 옵션)

2019. 12. 3. 11:20

request.body에 있는 데이터에 접근하기 위해 사용

//application/json 방식의 Content-Type 데이터
bodyParser.json();

//text/xml 방식의 Content-Type 데이터
bodyParser.text();

//application/x-www-form-urlencoded Content-Type 데이터
bodyParser.urlencoded({extended: true});

extended: true /false 차이점

  • true : qs모듈을 사용하여 쿼리 스트링 값을 해석
  • false : querystring 모듈을 사용하여 쿼리 스트링 값을 해석
let data = {
    info: {
        name: 'andy',
        age: 33
    },
    hobby[1]: sport,
    hobby[2]: coding
};

//qs.parse(data) 의 결과
{
    info: {
        name: 'sejoon',
        age: '30'
    },
    hobby: ['sport', 'coding']
}

//querystring.parse(data)의 결과
{
    'info[name]': 'andy',
    'info[age]': '33',
    'hobby[1]': 'sport',
    'hobby[2]': 'coding'
}

전달되는 객체(object)에 depth가 있을 경우에 인코딩 방식이 달라진다.

저작자표시 (새창열림)
    'nodejs' 카테고리의 다른 글
    • nodejs ejs render 대신 html 사용하기
    • youtube 영상 다운로드
    • req.body null 일때
    • nodejs 기본 메모리 제한
    joonsei
    joonsei
    IT 개발자의 소소한 기술 이야기.

    티스토리툴바