ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [bodyParser]
    React/프로젝트 2022. 1. 20. 22:25
    Client - Server 통신하는 법

    • body-parser
      • server에서 body-parser를 이용해서 client에서 보내준 자료(Body 데이터)를 분석(parse)해서 req.body출력
    • body-parser 설치
      • npm install body-parser --save
      • Express v4.16.0 기준으로 body parser가 빌트인 되어 별도 설치가 필요없다고 한다.
      • // Express 4.16+
        
        app.use(express.json());  // Used to parse JSON bodied
        app.use(express.urlencoded());  // Parse URL-encoded bodies
    • body-parser 사용방법
      • const express = require("express");
        // express사용 시 http모듈 따로 불러올 필요 없다.
        const app = express();
        // app 변수에 express() 할당 => app 구동
        const bodyParser = require("body-parser");
        
        app.use(bodyParser.urlencoded({ extended: true }));
        app.use(bodyParser.json());
    • bodyParser.urlencoded({ extended: true});
      • application/x-www-form-urlencoded방식의 Contect-Type데이터를 받아준다. (jQuery.ajax의 기본 타입)
    • bodyParser.json();
      • application/json방식의 Content-Type 데이터를 받아준다.
    • extended: true / false
      • 전달되는 객체(object)에 depth가 있을 경우 인코딩 방식이 달라진다. 
      • true : qs모듈을 사용하여 쿼리스트링 값 해석
      • false : querystring모듈을 사용하여 쿼리 스트링 값을 해석
      • [stackoverflow참고]

    'React > 프로젝트' 카테고리의 다른 글

    [로그인]  (0) 2022.01.24
    [Bcrypt] - 비밀번호 암호화  (0) 2022.01.21
    [Nodemon]  (0) 2022.01.21
    [express.Router]  (0) 2022.01.20
    [Node JS, Express JS]  (0) 2022.01.19
Designed by Tistory.