Express POST 데이터 송수신


Express 프레임워크를 사용해 클라이언트와 서버가 통신하는 Post 방식에 대한 방법이다. HTML 태그 중 <form>태그에서 <Input/> 태그 중 submit 타입의 버튼을 실행시키면 폼 태그 속성 중 'method'에 정의한 방법으로 'action' 속성에 정의한 위치로 전송한다.

 

 

index.html


index.html

//클라이언트
<form action="/insert-txt" method="post">
//POST방식으로 'localhost:XXXX/insert-txt'로 전송		
	<input type="text" name="id">		
	<button type="submit">submit</button>		
</form>

 

www.js


$ npm install body-parser --save
//POST BODY의 데이터 파싱을 위해 body-parser 설치

 

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

const bodyParser = require('body-parser');       //<-- 1. body-parser 선언
web.use(bodyParser.urlencoded({ extended: true })); //<--2. bodyParser.urlencoded설정
													//true : 객체안에 객체가 들어갈 것을 허용
                                                    
web.use('/', express.static(path.join(__dirname)));

web.get('/', (req, res)=>{
	res.sendFile(path.join(__dirname, 'index.html'));
});

/* ------------- POST 요청 받기 ------------- */
web.post('/insert-txt', (req, res)=>{
	
	const data = req.body;
	console.log(data);
	res.sendStatus(200);
    
})
/* ------------- POST 요청 받기 ------------- */

web.listen(port, () => {
    console.log('Express App on port ' + port + '!');
});

 

 

 

 

 

 

 

Node.js POST 방식으로 전송된 데이터 받는 방법

POST 방식의 데이터 전송받기 웹브라우저에서 서버로 데이터 전송 Form 태그 태그 웹브라우저에서 서버로 데이터를 전송하기 위해서는 HTML에서 form 태그를 사용해야 한다. form 태그는 값을 다루는

juni-official.tistory.com