์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- setTimeout()
- async
- webpack
- sql
- ํผํ
- https://m.blog.naver.com/tt2t2am1118/221010125300
- promise
- db
- addEventListener
- JS #ํ๋ก์ ํธ
- mysql
- https://youtube.com/playlist?list=PLuHgQVnccGMA5836CvWfieEQy0T0ov6Jh&si=FTaYv8m21EhO-A2K
- await
- slow and steady
- js
- database
- eport
- ์ฐธ๊ณ ๋ธ๋ก๊ทธ
- callback
- json
- ๊ฒ์
- ๋น๋๊ธฐ
- Import
- execCommand
- object
- Project
- Porject
- prj
- ajax
- ๋๊ธฐ
- Today
- Total
C-log
๐Nodejs : section30~33 - form ๊ทธ๋ฆฌ๊ณ response ๋ณธ๋ฌธ
์ด๋ฒ์๋ form์์ ๋ถํฐ response๊น์ง ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๊ฐ์ ๋ฐ์์ ์ฌ์ฉ์์๊ฒ ์๋ต์ ์ ๋ฌํ๋ ๋ชจ๋ ๊ณผ์ ๋ค์ ์ดํด ๋ณผ ๊ฒ์ด๋ค. ๋น๋ก ์ค์ data๋ฅผ ์ฒ๋ฆฌํ๋ sql์ด ์๋์ง๋ง ์ด๋ฅผ ํตํด์ ๋๋ต์ ์ธ ํ๋ฆ๊ณผ ๋งฅ๋ฝ์ ์ด๋์ ๋ ๊ฐ๋ ํด๋ณผ ์ ์์ ๊ฒ์ด๋ค.
form
์๋์ ๊ฐ์ด html์ ์์ฑํด๋ณด๊ณ ์คํ์ ํด๋ณด๊ณ URI์ ๊ฒฐ๊ณผ๋ฅผ ์ดํด๋ณด์.
<!-- form.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="http://localhost:3000/process_create">
<p><input type="text" name="title"></p>
<p>
<textarea name="description"></textarea>
</p>
<p>
<input type="submit">
</p>
</form>
</body>
</html>
URI
http://localhost:3000/process_create?title=test&description=test+des
* ์ฐธ๊ณ ๋ก ์์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป๊ธฐ ์ํด์๋ ๋ก์ปฌ ์๋ฒ๋ฅผ ๋๋ ค์ผ ํ๋ค. ๋ณธ์ธ์ live server๋ฅผ ์ฌ์ฉํ๋ค.
๊ฒฐ๊ณผ๋ฅผ ์ดํด๋ณด๋ฉด form์ผ๋ก ์ ์ก๋ input๊ฐ์ผ๋ก URI queryString์ ๋ง๋ค์ด๋ธ๋ค๋ ๊ฒ์ ์ ์ ์๋ค. ์ฆ, input์ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๊ฐ์ input type์ submit์ผ๋ก ์ ์ก์ ํ๋ฉด formํ๊ทธ์ action ์์ฑ์ผ๋ก ์ง์ ํ ์ฃผ์ URI์ queryString์ ๋ถ์ฌํ ๋ค๋ ๊ฒ์ ์ ์ ์๋ค. ์ด ๋ฐฉ๋ฒ์ ์ข์ ๋ฐฉ๋ฒ์ด ์๋๋ค. ๋ถํ์ํ ์ ๋ณด๋ฅผ ๋ด์์ ์น์ ์ ๋ณด๋ฅผ ๋ ธ์ถํ๋ ๊ฒ์ด๋ค. ์ฆ, ์ฐ๋ฆฌ๊ฐ Get์ผ๋ก id์ quryString ์ ๋ณด์ฌ์ฃผ๋ ๋ฐฉ์์ ๊ด์ฐฎ์ง๋ง post์ ๊ฐ์ ์ค์ํ ์ ๋ณด๋ฅผ ์๋ฒ์ ์ ๋ฌํ ๋๋ ์์ ๊ฐ์ ๋ฐฉ์์ ํ๊ฒ ๋๋ฉด ์ฌ์ฉ์์ ์ ๋ณด๊ฐ ๋ ธ์ถ ๋๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์ข์ง ์๋ค. ๋ฐ๋ผ์ ์๋์ ๊ฐ์ด ๋ฐ์ดํฐ ์ ์ก ๋ฐฉ์(method)์ ์ง์ ํด ์ฃผ๋ฉด ์๊น์ ๊ฐ์ด URI์ queryString์ด ์ธ๋ถ๋ก ๋ ธ์ถ ๋์ง ์๋๋ค.
<!-- form.html -->
<form action="http://localhost:3000/process_create" method="POST">
URI
http://localhost:3000/process_create
Create
๊ธ์ ์์ฑํ๋ ํ์ด์ง๋ฅผ ์ถ๊ฐํ ๊ฒ์ด๋ค. ๊ทธ๋ฌ๊ธฐ ์ํด์๋ ๊ธ์ ์์ฑํ๋ ํ์ด์ง๋ก ๋์ด ๊ฐ ์ ์๋ ๋งํฌ๊ฐ ํ์ํ๋ค. ์๋์ ๊ฐ์ด aํ๊ทธ๋ฅผ ์ถ๊ฐ ํ๋ฉด ๋๋ค.
/* main.js */
/* ์ดํ ์๋ต */
function templateHTML(title, list, body) {//list๋ ๋ฐ์์จ list๋ฅผ return์ ํตํด์ ์ ์๋๋ค.
return `
<!doctype html>
<html>
<head>
<title>WEB1 - ${title}</title>
<meta charset="utf-8">
</head>
<body>
<h1><a href="/">WEB</a></h1>
${list}
<a href="/create">create</a>
${body}
</body>
</html>
`;
}
/* ์ดํ ์๋ต */
์ง๋ ์๊ฐ์ ์ฐ๋ฆฌ๋ ์ฝ๋์ ํจ์จ์ ๋์ด๊ธฐ ์ํด์ ๋ ๋๋ง ๋์ด์ผํ๋ html๋ถ๋ถ์ ํจ์๋ก ๋ฐ๋ก ๋นผ๋ด์ template์ ์ ์ ํ๋ค. aํ๊ทธ๋ฅผ ์์ ๊ฐ์ ๊ณณ์ ์ถ๊ฐ ํด์ '/create' ํ์ด์ง๋ก ๋๊ฒจ ์ค ์ ์๊ฒ ์ค์ ์ ํด์ค๋ค. ์ดํ ํด๋น path์ ๋ง๋ ํ์ด์ง๋ฅผ createServer์ ์ถ๊ฐ ํด์ฃผ๋ฉด ๋๋ค. ์ถ๊ฐ ํด์ผํ path๋ ์๋์ ๊ฐ๋ค.
/* main.js */
//var app = http.createServer(function (request, response) { ~
else if (pathname === '/create') { //pathname(url.parse(_url, true).pathname)์ด create์ผ ๊ฒฝ์ฐ ์๋ ์ฝ๋ ์คํ
fs.readdir('./data', function (error, filelist) {
var title = 'WEB - create';
// var description = 'Hello, Node.js';
var list = templateList(filelist); //ํด๋น ๋งค๊ฐ๋ณ์๋ก ๋ฐ์์จ๋ค. ์ฌ๊ธฐ์ ์ดํด๊ฐ ์๊ฐ.
var template = templateHTML(title, list, `
<form action="http://http://localhost:3000/create_process" method="POST">
<p><input type="text" name="title" placeholder="title"></p>
<p>
<textarea name="description" placeholder="description"></textarea>
</p>
<p>
<input type="submit">
</p>
</form>`);//html ๊ตฌ๋ฌธ์ ์์ฑํ๋ ์ฝ๋
response.writeHead(200); //200์ด๋ web๋ธ๋ผ์ฐ์ ์ ์น์ธ์ผ๋ก ์์ฑ
response.end(template);
}
์ด๋ ๊ฒ ์์ฑํ๋ฉด ์ด์ '/create' pathName์ ๊ฐ์ง URI์ฃผ์๋ก ๋์ด๊ฐ ๊ธ์ ์์ฑ ํ ์ ์๋ ํ์ด์ง๊ฐ ์์ฑ์ด ๋๋ค. ์ฌ๊ธฐ์ ์ฃผ์ํด์ ๋ด์ผ ํ๋ ๋ถ๋ถ์ ์์์ ๋ค๋ฃจ์๋ formํ๊ทธ์ด๋ค. ์ญ์๋ ๊ธ์ ์์ฑํ๋ ํ์ด์ง๊ธฐ ๋๋ฌธ์ formํ๊ทธ์ ๋ฐ์ดํฐ ์ ์ก ๋ฐฉ์(method)๋ post์ฌ์ผํ๋ค. ์ด๋ฅผ ํตํด form ํ๊ทธ๋ก ๋ถํฐ ์ ์ก๋ ๋ฐ์ดํฐ๋ค์ ์๋ํ๊ฒ ๋๋ฉฐ ์ธ๋ถ๋ก ๋ถํฐ ๋ ธ์ถ๋์ง ์๊ฒ ๋๋ค.
create_process(doCreate)
์ด์ ๊ธ์ ์์ฑํ๋ ํ์ด์ง๋ฅผ ๋ง๋ค์๋ค๋ฉด ์์ฑ๋ ๊ธ์ ์๋ฒ๋ก ๋ณด๋ด๋ ํ์ด์ง๊ฐ ํ์ํ๋ค. ์์ ์ ์ฝ๋์์ formํ๊ทธ์ action์ URI๋ '/create_process'๋ก ๋์ด ์๋ค. ์ฐ๋ฆฌ๊ฐ createํ์ด์ง๋ฅผ ๋ง๋ค์๋ ๊ฒ ์ฒ๋ผ ์ด๋ฒ์๋ ๊ฐ๊ฒ ์์ฑํ๋ฉด ๋๋ค.
/* main.js */
var qs = require('querystring'); //๋ชจ๋
//var app = http.createServer(function (request, response) { ~
else if (pathname === '/create_process') { //create์์ POSTํ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
let body = ''
request.on('data', function (data) {
body = body + data
})
request.on('end', function () {
let post = qs.parse(body) //querystring ๋ชจ๋์ ํตํด์ create์์ post๋ก ์ ์ก๋ฐ์ ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
var title = post.title;
var description = post.description;
console.log(post)
})
response.writeHead(200); //200์ด๋ web๋ธ๋ผ์ฐ์ ์ ์น์ธ์ผ๋ก ์์ฑ
response.end('success');
}
์์ฝ๋๋ฅผ ์ ์ฌํ ๋ณด๋ฉด body๋ผ๋ ๋ณ์๊ฐ ์๋ค. ์ ๋ณ์๋ ๋ด๊ฐ ์๋ฒ์ ์ ์กํ data๋ฅผ ๋ด์๋ด๋ ์ญํ ์ด๋ผ๊ณ ๋ณด๋ฉด ๋๋ค. ์ค์ API์ ํต์ ์์ Post๋ฅผ ํ ๋ ๋ฐฑ์๋์์ body์ ์ด๋ค ๊ฐ์ ๋ฃ์ด์ผ ํ๋์ง ๊ท์น๊ณผ ์์๋ค์ ์ค์ ํ๋๋ฐ ์์ ์ฝ๋๊ฐ ์ด์ ๊ฐ์ ์ญํ ์ ํ๋ ๊ฒ์ด๋ค. ์๋ ์ฝ๋๋ฅผ ํตํด์ ์์ธํ ์ฌํญ๋ค์ ์ดํด ๋ณผ ์ ์๋ค.
create_process(doCreate) ์ฝ๋์ ๋ถ์
๊ฒฝ๋ก ํ์ธ
/* main.js */
else if (pathname === '/create_process') { ~
- ์ด ์กฐ๊ฑด๋ฌธ์ URL์ ๊ฒฝ๋ก๋ช ์ด /create_process์ผ ๋๋ง ์คํ๋๋๋ก ํ๋ค. ์ด ๊ฒฝ๋ก๋ ํผ ๋ฐ์ดํฐ(formData)๊ฐ ์๋ฒ๋กpost๋๋ ์๋ํฌ์ธํธ๋ก ์ฌ์ฉ๋๋ค.
๋ฐ์ดํฐ ์์
/* main.js */
let body = '';
request.on('data', function (data) {
body += data;
});
- body ๋ณ์๋ฅผ ์ด๊ธฐํํ์ฌ ์ฌ์ฉ์๋ก๋ถํฐ ์์ ๋ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ค.
- request.on('data', callback) ๋ฆฌ์ค๋๋ ์ฌ์ฉ์๋ก๋ถํฐ ๋ฐ์ดํฐ๊ฐ ์คํธ๋ฆผ ํํ๋ก ๋์ฐฉํ ๋๋ง๋ค ํธ์ถ๋๋ค. ์ด ๋ ์์ ๋ data๋ฅผ body์ ์ฝ์ ํ๋ค.
๋ฐ์ดํฐ ์ฒ๋ฆฌ ์๋ฃ
/* main.js */
request.on('end', function () {
let post = qs.parse(body);
var title = post.title;
var description = post.description;
console.log(post);
});
- request.on('end', callback) ์ด๋ฒคํธ๋ ๋ฐ์ดํฐ ์์ ์ด ์๋ฃ๋์์ ๋ ํธ์ถ๋๋ค. ์ด ์์ ์์ ๋ชจ๋ ๋ฐ์ดํฐ๊ฐ body ๋ณ์์ ์ ์ฅ๋์ด ์๋ค.
- qs.parse(body)๋ฅผ ์ฌ์ฉํ์ฌ URL ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด body๋ฅผ JavaScript ๊ฐ์ฒด๋ก ํ์ฑํ๋ค. qs๋ querystring ๋ชจ๋์ ์ผ๋ฐ์ ์ธ ๋ณ์นญ์ด๋ฉฐ ์ด ๊ฐ์ฒด์์ title๊ณผ description ํ๋๋ฅผ ์ถ์ถํ๋ค.
- ์ฐธ๊ณ ๋ก ์๋์ ๊ฐ์ด console๋ก ํ์ธ์ ํด๋ณด๋ฉด ์ด์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ด ๋ผ ์ ์๋ค.
-
/* main.js */ request.on('end', function () { console.log("body: ", body); console.log("queryString: ", qs); let post = qs.parse(body); var title = post.title; var description = post.description; console.log("post: ", post); })
-
body: title=%ED%85%8C%EC%8A%A4%ED%8A%B8+%EC%9E%85%EB%8B%88%EB%8B%A4.&description=%ED%85%8C%EC%8A%A4%ED%8A%B8+%EC%84%A4%EB%AA%85+%EC%9E%85%EB%8B%88%EB%8B%A4. queryString: { unescapeBuffer: [Function: unescapeBuffer], unescape: [Function: qsUnescape], escape: [Function: qsEscape], stringify: [Function: stringify], encode: [Function: stringify], parse: [Function: parse], decode: [Function: parse] } post: [Object: null prototype] { title: 'ํ ์คํธ ์ ๋๋ค.', description: 'ํ ์คํธ ์ค๋ช ์ ๋๋ค.' }
์๋ต
/* main.js */
response.writeHead(200); //200์ด๋ web๋ธ๋ผ์ฐ์ ์ ์น์ธ์ผ๋ก ์์ฑ
response.end('success');
- response.writeHead(200)๋ก HTTP ์ํ ์ฝ๋ 200์ ํด๋ผ์ด์ธํธ์ ์ ์กํฉ๋๋ค. 200์ ์์ฒญ์ด ์ฑ๊ณต์ ์ผ๋ก ์ฒ๋ฆฌ๋์๋ค๋ ์๋ฏธ์ ๋๋ค.
- response.end('success')๋ก ์๋ต์ ์ข ๋ฃํ๊ณ ํด๋ผ์ด์ธํธ์๊ฒ 'success'๋ผ๋ ๋ฉ์์ง๋ฅผ ๋ณด๋ ๋๋ค.
post๋ก ๋ฐ์ ๋ฐ์ดํฐ๋ก ํ์ผ ์์ฑํ๊ธฐ fs.writeFile
์ด์ create_process๋ฅผ ํตํด์ ์ฌ์ฉ์๋ก ๋ถํฐ post๋ก ๋ฐ์ ๋ฐ์ดํฐ ๊ฐ๋ค์ ๋ฐ์ ์ฌ ์ ์๋ ์ฝ๋๋ฅผ ์์ฑ ํ๋ค. ์ด์ ๋จ์๊ฑด ํด๋น ๋ฐ์ดํฐ๋ค์ ๊ฐ์ง๊ณ dataํด๋์ post๋ก ๋ฐ์์จ ๋ฐ์ดํฐ ๊ฐ๋ค๋ก ํ์ผ์ ์์ฑํ๋ ์ฝ๋๋ฅผ ์ถ๊ฐ์ ์ผ๋ก ์์ฑํ ๊ฒ์ด๋ค. ์ถ๊ฐ๋ ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
/* main.js */
// request.on('end', function () { ~
// ...์ดํ ์๋ต...
var description = post.description;
// console.log(post)
fs.writeFile(`data/${title}`, description, 'utf8', (err) => {
response.writeHead(302, { Location: `/?id=${title}` }); //200์ด๋ web๋ธ๋ผ์ฐ์ ์ ์น์ธ์ผ๋ก ์์ฑ
response.end();
})
// ...์ดํ ์๋ต...
ํ์ผ ์ฐ๊ธฐ()
- fs.writeFile ํจ์๋ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ํ์ผ์ ๊ฒฝ๋ก์ ์ด๋ฆ์ ๋ฐ๋๋ค. ์ฌ๊ธฐ์๋ data/${title}์ ์ฌ์ฉํ์ฌ data ๋๋ ํ ๋ฆฌ ๋ด์ title ๋ณ์์ ๊ฐ์ ํด๋นํ๋ ์ด๋ฆ์ผ๋ก ํ์ผ์ ์์ฑํ๊ฑฐ๋ ๋ฎ์ด์ฐ๊ธฐ ํ๋ค.
- ๋ ๋ฒ์งธ ์ธ์์ธ description์ ํ์ผ ์์ ์ธ ๋ด์ฉ์ด๋ค.
- ์ธ ๋ฒ์งธ ์ธ์ 'utf8'์ ํ์ผ์ ์ธ ๋ ์ฌ์ฉํ ์ธ์ฝ๋ฉ ๋ฐฉ์์ ์ง์ ํ๋ค.
- ๋ค ๋ฒ์งธ ์ธ์๋ ์ฝ๋ฐฑ ํจ์๋ก ํ์ผ ์ฐ๊ธฐ ์์ ์ด ์๋ฃ๋์์ ๋ ์คํ๋๋ค. ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด err ํ๋ผ๋ฏธํฐ์ ์๋ฌ ๊ฐ์ฒด๊ฐ ์ ๋ฌ๋ฉ๋๋ค.
์๋ต ์ฒ๋ฆฌ
- ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์์ response.writeHead(302, { Location: /?id=${title} });๋ฅผ ํธ์ถํ์ฌ HTTP ์ํ ์ฝ๋ 302(๋ฆฌ๋ค์ด๋ ์ -์ฝ๋์ ๋ฆฌ์์ค์ ์์น๊ฐ ๋ฐ๋์๋ค.๋ ๋ป)์ ํจ๊ป Location ํค๋๋ฅผ ์ค์ ํ๋ค. Location์ ์ฌ์ฉ์๊ฐ ๋ฆฌ๋ค์ด๋ ์ ๋ ์๋ก์ด URL์ ์ง์ ํ๋ค. ์ฌ๊ธฐ์๋ ์ฟผ๋ฆฌ ์คํธ๋ง์ผ๋ก id ํ๋ผ๋ฏธํฐ์ title์ ๊ฐ์ ์ฃผ์ด ํ์ผ์ด ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ ๋ ์ฌ์ฉ์๋ฅผ ํด๋น ํ์ผ์ ํ์ด์ง๋ก ๋ฆฌ๋ค์ด๋ ํธ ์ํต๋๋ค.
- response.end()๋ HTTP ์๋ต์ ์ข ๋ฃํ๊ณ ์ค์ ๋ ํค๋์ ๋ณธ๋ฌธ์ ์ฌ์ฉ์์๊ฒ ์ ์กํ๋ค. ์ด ๊ฒฝ์ฐ ๋ณ๋์ ๋ณธ๋ฌธ ๋ด์ฉ์ ์ ์กํ์ง ์์ผ๋ฏ๋ก ๋จ์ํ ์๋ต์ ๋ง๋ฌด๋ฆฌ ์ง๋๋ค.
์๋ ํด๋ณผ ์ ์๋ response
post๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ด๋ฃจ์ด ์ง๋ฉด ์ฌ์ฉ์์๊ฒ response๋ก title๊ณผ description์ ์ ๋ฌํด๋ณด์๋ฉด ์๋์ ๊ฐ์ด ์์ฑํ๋ฉด ๋๋ค.
//json ํ์ผ ํ์์ผ๋ก ์๋ต
fs.writeFile(`data/${title}`, description, 'utf8', (err) => {
if (err) {
response.writeHead(500, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ error: 'Failed to write file' }));
} else {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ title: title, description: description }));
}
});
์ฌ๊ธฐ์ ์ ์ํด์ผ ํ ์ ์ ํฌ๋ฉง ์ค์ ์ด๋ค. ํฌ๋ฉง์ ๋ฐ๋ผ์ Content-Type์ ์ ์ค์ ํด์ผ ํ๋ค. ํ์ฌ๋ JSON ์๋ต์ผ๋ก ์ฃผ๋ ๊ฒฝ์ฐ์ด๊ธฐ ๋๋ฌธ์ application/json์ผ๋ก ์ค์ ํ๋ค. ๋ง์ฝ html๋ก ์๋ต์ ํ ๊ฒฝ์ฐ text/html๋ก ์ค์ ํ๋ฉด ๋๋ค.
'๐Nodejs > โกver.0' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐Nodejs : section28(1~3) - ๋๊ธฐ์ ๋น๋๊ธฐ (0) | 2024.06.24 |
---|---|
๐Nodejs : plus Alpha - ๋ถ์ํ๊ธฐ (0) | 2024.06.17 |
๐Nodejs : section26 - ํจ์๋ฅผ ์ด์ฉํด์ ์ ๋ฆฌ ์ ๋ํ๊ธฐ (0) | 2024.06.17 |
๐Nodejs : section24 - readdir์ผ๋ก ํ์ผ๋ชฉ๋ก ๋ง๋ค๊ธฐ (0) | 2024.01.11 |
๐Nodejs : section19 - NotFound์ฒ๋ฆฌ์ ํํ์ด์ง ์ ์๊ตฌํ (0) | 2024.01.11 |