-
express.urlenconded()JavaScript/Exress 2022. 10. 11. 20:11
출처 : https://kirkim.github.io/javascript/2021/10/16/body_parser.html 헤더에서 규정되는 content-type을 보라.
x-www-form-urlencoded 형태를 받는다.
위의 예시는 클라이언트에서 form 태그를 통해 전송되었다.
이 말은 곧, form 태그로 전송되는 값은 express.json() 이 아니라 express.urlexcoded()로 받아야 한다는 것.
더보기This is a built-in middleware function in Express. It parses incoming requests with urlencoded payloads and is based on body-parser.
내장된 미들웨어. "urlencoded payload"로 들어오는 리퀘스트를 파싱.
body-parser 기반.
지금은 express 패키지 안에 body-parser 미들웨어가 포함되어 있으므로 따로 body-parser를 임포트할 이유가 없다.
const bodyParser = require('body-parser'); app.use(bodyParser.urlencoded(extended: true);
더보기A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body), or an empty object ({}) if there was no body to parse, the Content-Type was not matched, or an error occurred.
파싱된 데이터를 포함하는 새로운 객체 request 는 미들웨어를 거쳐 객체에 채워짐.(req.body)
파싱할 본문 이 없거나 일치하지 않거나 오류가 발생한 경우 req.body빈 객체( )가 채워진다.
extended 옵션
더보기This object will contain key-value pairs, where the value can be a string or array (when extended is false), or any type (when extended is true).
1. extended : false
nodeJS 에 내장된 querystring 모듈을 사용.
이 때 req 객체는 key value 쌍을 contain 하는데, 여기서 value는 문자열이나 배열일 수 있다. (extended 옵션을 false로 했을 때)
대강 이런 형태로 들어온다.
[Object: null prototype] { sample: 'hello world!' }
2.extende : true
qs 모듈을 사용한다.
qs 모듈은 express에 포함되어 자동으로 설치된다. (궁금하면 package.json을 열어보라는 뜻이다.)
vlaue는 지정되지 않은 타입(any)으로 받을 수 있다. (extended 옵션을 true 로 했을 때.)
대강 이런 형태로 들어온다.
{ sample: 'hello world!' }
https://expressjs.com/en/5x/api.html#express.urlencoded
Express 5.x - API Reference
Express 5.x API Note: This is early beta documentation that may be incomplete and is still under development. express() Creates an Express application. The express() function is a top-level function exported by the express module. const express = require('
expressjs.com
'JavaScript > Exress' 카테고리의 다른 글
에러로그 : winston, morgan (0) 2022.10.12 express.router (0) 2022.10.11 Express) app 객체, request 객체 (0) 2022.10.01