-1
노드를 학습하고 문제가 있습니다. POST 요청시 데이터를 읽지 않는 이유는 무엇입니까? req.method POST가 작동하지만, '데이터'를 내보내는 것이 작동하지 않으면 html로 POST를 보냅니다.POST에서 데이터를 읽지 않는 이유
<form method="post" action='./script.js'>
<label for="my-form">Enter phraze</label><input type="text" id='my-form'><br>
<input type="submit">
</form>
let fs = require('fs');
let http = require('http');
let server = http.createServer((req, res) => {
res.setEncoding = 'utf8';
res.writeHead(200, {'Content-type': 'text/html'});
if(req.url == '/'){
reply('./index.html', res)
}
if(req.method == 'POST'){
let body;
req.on('data',
(chunk) => { if(chunk != null) {body += chunk.toString(); res.write(chunk.toString())} })
req.on('end',
() => res.end(body))
}
}).listen(5858);
server.on('error', (err) => { throw err.message });
function reply(path, res){
let streamPage = fs.createReadStream(path, {encoding: 'utf8'});
streamPage.on('readable',
(chunk) => { chunk = streamPage.read(); if(chunk != null) res.write(chunk) });
streamPage.on('end',
(out) => res.end());
streamPage.on('error', (err) => err.message)
}
와 같이 설정되었습니다. 하지만 작동하지 않습니다. 내 스크립트 파일에서 나는 '데이터'에 내 콜백에 entred하지 않습니다 –
@ КостяМатюшонок 그것은 내 실수입니다, 미안 해요. HTML은 노드 서버 라우팅에 대해 아무것도 모릅니다. CURL 또는 fiddler를 통해 게시물 요청을 보내면 콜백에 입장합니다. HTML 페이지를 통해 요청을 보내려면 action = '/'을 action = 'http : // localhost : 5858 /'로 바꾸거나 express.js를 사용하여 유용한 라우팅 및 템플릿을 만드십시오. –
방법을 확인하십시오.하지만 작동하지 않습니다 (( –