ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Nodejs Express bodyParser
    Search: NodeJs NodeJs 2023. 1. 15. 13:56

     

    Nextjs bodyParser

    multipart form-data 와 boundary

     

    const express = require('express');
    const bodyParser = require('body-parser');
    
    let app = express();
    
    switch (2) {
    	case 1:
    		app.use(express.json()); //for parsing application/json
    		//result: Dbg req.body typeof: object value: {}
    		break;
    	case 2:
    		//app.use(express.json()); //for parsing application/json
    		app.use(express.urlencoded({ extended: false })); //for parsing application/x-www-form-urlencoded
    		//result: req.body typeof: object value: {"id":"id1"}
    		break;
    	case 3:
    		app.use(express.json()); //for parsing application/json
    		app.use(express.urlencoded({ extended: false })); //for parsing application/x-www-form-urlencoded
    		//result: req.body typeof: object value: {"id":"id1"}
    		break;
    	case 4:
    		app.use(express.json()); //for parsing application/json
    		app.use(express.urlencoded({ extended: true })); //for parsing application/x-www-form-urlencoded
    		//result: req.body typeof: object value: {"id":"id1"}
    		break;
    	case 5:
    		app.use(bodyParser.json()); // parse application/json
    		app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded
    		//result: req.body typeof: object value: {"id":"id1"}
    		break;
    	case 6:
    		app.use(bodyParser.json()); // parse application/json
    		app.use(bodyParser.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded
    		//result: req.body typeof: object value: {"id":"id1"}
    		break;
    }
    
    app.post('/login', function (req, res, next) {
    	console.log("Dbg req.body typeof: " + (typeof req.body) + " value: " + JSON.stringify(req.body));
    	res.json(req.body);
    });
    
    app.get('/', (req, res) => {
    	res.send(`
    	<h2> test parser </h2>
    	<form action="/login" method="post">
    	<input type="text" name="id" value="id1">
        <button type="submit">제출</button>
    	</form>
    	`);
    });
    
    app.listen(3000, () => {
    	console.log('Server listening on http://localhost:3000 ...');
    });

    case 1번만 req.body에 값이 없고 나머지는 모두 req.body에 값이 있다.
    (6번의 경우 req.body값이 들어 있으나 { extended: false }로 인하여 조금 다른 방식의 값이다.)

     

    nodejs는 기본으로는 server측의 req수신 시 body.data에 과련된 값은 req.on('data', ()=>{})를 통해서 값을 직접 읽어서 사용하는 방식이다(호출 된 함수 안에서 myclass1 = qs.parse(data)의 방식으로 객체에 값 넣음). 그래서 req.body가 없다(필요 없기에).

    Nodejs request.on() data, end Ex

     

    nodejs app에 body parser를 등록하면 req.body에 값이 자동으로 들어가 있다

    nextjs는 기본적값으로 body parser를 사용한다. 외부 모듈로 parser하고 싶으면 { extended: false }로 해야 한다.
    Nextjs bodyParser

     

    'NodeJs' 카테고리의 다른 글

    Next.js Nuxt.js Nest.js 차이  (0) 2023.01.27
    Bootstrap 설치하기  (0) 2023.01.22
    Formidable IncomingForm() 값 Ex  (0) 2023.01.16
    Nodejs request.on() data, end Ex  (0) 2023.01.15
    Fs(file system) module  (0) 2023.01.14
    http status codes를 enum 이름으로 처리  (0) 2022.12.25
    Styled-components  (1) 2022.12.24
    Module not found: Can't resolve 'fs'  (0) 2022.12.24

    댓글