(1)~(4)๊น์ง๋ ์๋ฒ๋ฅผ ์คํ ์ ํ๋ ๋ฐฉ๋ฒ๊ณผ, ๋ค์ด์ค๋ ์์ฒญ์ ๋ํ ์ ๋ณด๋ฅผ ๋ด์ ์์ฒญ ๊ฐ์ฒด๋ฅผ ๋ฐ๊ณ , ์๋ต์ ๋๋ ค์ค ์ ์๋ ์๋ต ๊ฐ์ฒด์ ๋ํ ํฌ์คํ ์ ์์ฑํ์๋ค.
ํฐ๋ฏธ๋ ์๋ฒ ์ข ๋ฃ ctrl + c ์๋ฒ ์ฌ์์ node app.js |
1. ์ค์ต์ฉ ์น์๋ฒ ์ฝ๋ ์์ฑ
์๋์ ๊ฐ์ด ์ ๋ ฅํด์ค๋ค.
const url = req.url; url์ด ๋ญ์ง์ ๋ฐ๋ผ์ ๋ค๋ฅธ ๋ด์ฉ์ ํด๋ผ์ด์ธํธ์๊ฒ ๋ณด๋ด์ฃผ๋๋ก ํ์.
if (url === '/') {
const http = require('http');
const server = http.createServer((req, res) => {
const url = req.url;
if (url === '/') {
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write(
'<body><form action="/message" method="POST"><input type="text" name="message"><button = "submit">Send</button></form></body>');
res.write('</html>');
return res.end();
}
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write('<body><h1>Hello from my Node.js Server!</h1></body>');
res.write('</html>');
res.end('Hello Node'); //์ด ์์ ๋ถํฐ๋ ๋ ์ด์ ์๋ฌด ๊ฒ๋ ์
๋ ฅํด์๋ ์๋จ
//์ด ๋ถ๋ถ์ ์ฐ๋ฆฌ๊ฐ ํด๋ผ์ด์ธํธ๋ก ๋ค์ ์ ์กํ๋ ๋ถ๋ถ์ผ๋ก, Node.js๊ฐ ํด๋ผ์ด์ธํธ๋ก ๋ค์ ์ ์กํ๊ธฐ ๋๋ฌธ
});
server.listen(3000);
์ฃผ์์ฐฝ์ http://localhost:3000/ ํน์ http://localhost:3000
์์์ ๋ถ๊ธฐ์ฒ๋ฆฌ๋ฅผ ๋ ๋๋ ค๋ณธ ์์
const http = require('http');
const server = http.createServer((req, res) => {
const url = req.url;
if (url === '/') { //url์ด ์๋ฌด๋ฐ ๊ฒฝ๋ก๊ฐ ์๋ค๋ฉด,
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write(
'<body><form action="/message" method="POST"><input type="text" name="message"><button = "submit">Send</button></form></body>');
res.write('</html>');
return res.end();
} else if(url === '/Course') {
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write('<body><h1>course</h1></body>');
res.write('</html>');
return res.end();
}
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write('<body><h1>Hello from my Node.js Server!</h1></body>');
res.write('</html>');
res.end('Hello Node'); //์ด ์์ ๋ถํฐ๋ ๋ ์ด์ ์๋ฌด ๊ฒ๋ ์
๋ ฅํด์๋ ์๋จ
//์ด ๋ถ๋ถ์ ์ฐ๋ฆฌ๊ฐ ํด๋ผ์ด์ธํธ๋ก ๋ค์ ์ ์กํ๋ ๋ถ๋ถ์ผ๋ก, Node.js๊ฐ ํด๋ผ์ด์ธํธ๋ก ๋ค์ ์ ์กํ๊ธฐ ๋๋ฌธ
});
server.listen(3000);
2. ๋ฆฌ๋ค์ด๋ ์
์ด์ GET ์์ฒญ ๋ง๊ณ POST ์์ฒญ์ ๋ค๋ฃจ๋๋ก ํด๋ณธ๋ค.
request method์์ ๋ฉ์๋๋ฅผ ํ์ฑํ๊ณ ์๋์ ์กฐ๊ฑด์ ์ถ๊ฐํด๋ณด์.
else if (url === '/message' && method === 'POST')
์ด์ ํด๋น ์กฐ๊ฑด๋ฌธ์ ๋ฟ์ผ๋ฉด ์ ์ ๋ฅผ ๋ค์ /๋ก ๋ณด๋ด์ด Course์ ๋จ๊ฒจ๋์ง ์์ผ๋ ค๊ณ ํ๋ค!
๋์ ์๋ก์ด ํ์ผ์ ์์ฑํ์ฌ ์ ์ ๊ฐ ์ ๋ ฅํ ๋ฉ์์ง๋ฅผ ์ ์ฅํด์ฃผ๋๋ก ํ๋ค.
๊ฒฝ๋ก ์ฌ์ง์ ๊ณผ ํ์ผ ์์ฑ๋ถํฐ ์์ํด๋ณธ๋ค.
๋ค๋ฅธ ํจํค์ง, ๋ค๋ฅธ ์ฝ์ด ๋ชจ๋์ด ํ์ํ๋ค. (ํ์ผ์์คํ ์ฝ์ด๋ชจ๋) ์์์ ์ ์ฅํด์ ๋ถ๋ฌ์ ์ํ๋ ์ด๋ฆ์ ๋ถ์ฌ๋ ๋๋ค.
์ด ํฌ์คํ ์์๋ fs
const fs = require('fs');
wirteFile์ ํ์ผ๋ก ๊ฐ๋ ๊ฒฝ๋ก๋ฅผ ์ทจํ๊ฒ ๋๋ฉฐ ์ด๋ฆ์ ๊ฐ๋จํ๊ฒ message.txt
writeHead๋ ํ๋ฒ์ ๋ช๊ฐ์ง ๋ฉํ ์ ๋ณด๋ฅผ ์์ฑํ ์ ์๊ฒ ํ๋ฉฐ
๋ค์์ผ๋ก ๊ฒฝ๋ก ์ฌ์ง์ ์ ์๋ฏธํ๋ ์ํ์ฝ๋ 302 ์ค์ , ๋๋ฒ์งธ ์ธ์๋ก๋ js ๊ฐ์ฒด์ ์ค์ ํ๊ธธ ์ํ๋ ํค๋ ์ ๋ฌ
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
const url = req.url;
const method = req.method;
if (url === '/') { //url์ด ์๋ฌด๋ฐ ๊ฒฝ๋ก๊ฐ ์๋ค๋ฉด,
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write(
'<body><form action="/message" method="POST"><input type="text" name="message"><button = "submit">Send</button></form></body>');
res.write('</html>');
return res.end();
} else if (url === '/message' && method === 'POST') {
fs.writeFileSync('message.txt', '๋๋ฏธํ
์คํธ');
//res.writeHead(302, {});
res.statusCode = 302;
res.setHeader('Location', '/')//๋ธ๋ผ์ฐ์ ๊ฐ ์๋ฝํ๋ ๋ํดํธ ํค๋,
//์์น์ ๊ฒฝ์ฐ /๋ก ์ค์ ํ๊ฒ ๋๋ฉด ๋ด๊ฐ ์ด๋ฏธ ์คํ ์ค์ธ ํธ์คํธ๋ฅผ ์๋์ผ๋ก ์ฌ์ฉํ๊ฒ ๋๋ค.
return res.end();
}
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write('<body><h1>Hello from my Node.js Server!</h1></body>');
res.write('</html>');
res.end('Hello Node'); //์ด ์์ ๋ถํฐ๋ ๋ ์ด์ ์๋ฌด ๊ฒ๋ ์
๋ ฅํด์๋ ์๋จ
//์ด ๋ถ๋ถ์ ์ฐ๋ฆฌ๊ฐ ํด๋ผ์ด์ธํธ๋ก ๋ค์ ์ ์กํ๋ ๋ถ๋ถ์ผ๋ก, Node.js๊ฐ ํด๋ผ์ด์ธํธ๋ก ๋ค์ ์ ์กํ๊ธฐ ๋๋ฌธ
});
server.listen(3000);
๋ค์ ์๋ฒ๋ฅผ ๋๋ฆฌ๊ณ ์๋ฌด ๊ฐ์ด๋ ์ ๋ ฅํ๊ณ ๋ฒํผ ๋๋ ์ ๋์ ๋คํธ์ํฌ ํญ
ํธ์ง๊ธฐ์์๋ message.txt ๊ฐ ์ ์์ฑ๋์ด ์์ ๊ฒ์ ํ์ธํ ์ ์๋ค!
์ฐฝ์ ๋น์ฐํ
3. ์ ์ ์ ๋ ฅ๊ฐ์ ํ์ผ ์์ฑํ์ฌ ์ ์ฅํ๊ธฐ
์ด์ ์ ์ ๊ฐ ๋ณด๋ธ ๋ฐ์ดํฐ๋ฅผ ์ค์ ๋ก ํ์ฑํ๊ณ ํด๋น ๋ฐ์ดํฐ๋ฅผ ํ์ผ์ ์ฐ๋ ์์ ์ ํด๋ณด๋๋ก ํ๋ค.
๋ค์ด์ค๋ ์์ฒญ์ ๋ถ์ํด์ ์์ฒญ์ ์ผ๋ถ์ธ ๋ฐ์ดํฐ๋ฅผ ์์๋ณด๋๋ก ํ๋ค.
๋ค์ด์ค๋ ์์ฒญ์ ๋ฐ์ดํฐ ์คํธ๋ฆผ์ผ๋ก ๋ณด๋ด์ง๋ค..
* ๋ฐ์ดํฐ ์คํธ๋ฆผ์ด๋?
- ์คํธ๋ฆผ์ ์ง์์ ์ธ ํ๋ก์ธ์ค์ด๋ค.
๋ ธ๋๊ฐ ๋ง์ ์์ ์์ฒญ์ ํ ์ฒญํฌ์ฉ ์ฝ๊ณ ์ด๋ ์์ ์๋ ๋ค ์ฝ๊ฒ ๋ ๊ฒ์ด๋ค.
์๋ฌด ๊ฒ๋ ์ ํ๋ฉด์ ๊ธฐ๋ค๋ฆด ํ์๊ฐ ์๋ค.
๋ฒํผ๋ ๋ฒ์ค ์ ๋ฅ์ฅ๊ณผ ๊ฐ๋ค. //๋ฒ์ค๋ ๊ณ์ ๋ฌ๋ฆฌ๊ฒ ์ง๋ง ์ฌ์ฉ์๊ฐ ์ค๊ฐ์๋ ์ฐพ์ ๋ค์ด๊ฐ ์ ์๋..
์ฌ๋ฌ ๊ฐ์ ์ฒญํฌ๋ฅผ ๋ณด์ ํ๊ณ ํ์ฑ์ด ๋๋๊ธฐ ์ ์ ์์ ํ ์ ์๋๋ก ํ๋ค.
๊ฐ๋ ์ด ์ถ์์ ์ผ ์ ์์ง๋ง, ๋ฒํผ๋ ๋ค๋ฃจ๊ธฐ ์ฝ๋ค!
๋ฐ๋ก ์ฝ๋๋ก ๋์๊ฐ๋ณด์
req.on(data)๋ฅผ ํธ์ถํ๋ฉด ๋ฆฌ์ค๋๊ฐ ๋ฐ์ดํฐ ์ฒญํฌ๋ฅผ ๋ฐ๋๋ก chunk๋ฅผ ์ ๋ ฅํ๋ค.
์ด์ ์ด chunk๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌด์ธ๊ฐ๋ฅผ ํด์ผ๋ง chunk์ ์ํธ์์ฉํ ์ ์๋ค.
req.on(end)๋ฅผ ํธ์ถํ๋ฉด ๋ค์ด์ค๋ ์์ฒญ ๋ฐ์ดํฐ ํน์ ๋ค์ด์ค๋ ์ ๋ฐ์ ์ธ ์์ฒญ์ ๋ถ์ํ ํ์ ๋ฐ์ํ๋ค.
req.on('data', (chunk) => {
console.log(chunk);
body.push(chunk); //์ด์ ์์ฒญ์ ๋ํ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ป์ ๋๊น์ง ํจ์๊ฐ ์คํ๋ ๊ฒ
});
req.on('end');
์๋์ ๊ฐ์ ์ฝ๋์์ const parsedBody = Buffer.concat(body); ๋ผ์ธ์ด ์ฃผ๋ ์๋ฏธ๋,
์ ๋ฒํผ๊ฐ ์์ฑ๋๊ณ ๋ณธ๋ฌธ ์์ ์๋ ๋ชจ๋ ์ฒญํฌ๊ฐ ์ถ๊ฐ๋ ๊ฒ์ด๋ค.
if (url === '/message' && method === 'POST') {
//์์ฒญ ๋ณธ๋ฌธ์ ์ง์ ๊ฒ์ด๋ฏ๋ก ์์๋ช
body๋ก ์์ฑ ํ, ๋น ๋ฐฐ์ด๋ก ๋ง๋ค์ด ์ฃผ์.
const body = [];
//post ๋ฉ์์ง๋ฅผ ๋ฐ์ ๋ ์๋ต์ ๋ณด๋ด๊ฑฐ๋ ํ์ผ์ ์ฐ๊ธฐ ์ ์ ์์ฒญ ๋ฐ์ดํฐ ๋ฐ์ผ๋ ค๋ฉด
//req.๋ฅผ ์
๋ ฅํด์ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ๋ฑ๋กํ๋ฉด ๋๋ค.
//createServer๊ฐ ๋์ ์๋ฒ๋ฅผ ์์ฑํด์ฃผ๋ ๋ฐ๋ฉด, on ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ง์ ํ ์ ์๋ค.
req.on('data', (chunk) => {
console.log(chunk);
body.push(chunk); //์ด์ ์์ฒญ์ ๋ํ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ป์ ๋๊น์ง ํจ์๊ฐ ์คํ๋ ๊ฒ
});
req.on('end', () => {
const parsedBody = Buffer.concat(body);
// ์ฒญํฌ๋ค์ ๋ฐ์ ๋ค์์ ์ฒญํฌ๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํด์๋ ๋ฒํผ(๋ฒ์ค์ ๋ฅ์ฅ) ๊ฐ์ ์ฅ์์์ ์ํธ์์ฉ ํ ์ ์์ ๊ฒ์ด๋ค.
});
parsedBody๋ผ๋ ๋ฒํผ์ toString์ ํธ์ถํด ๋ฌธ์์ด๋ก ์ ํํ๋ค.
์ด์ ๋ฒ์ค์ ๋ฅ์ฅ(๋ฒํผ)์ ๋ฒ์ค๊ฐ ๊ธฐ๋ค๋ฆฌ๊ณ ์๋ค.
์ด์ ๋ฒํผ๋ฅผ ๋ฌธ์์ด๋ก ์ ํํ๋๋ฐ ์์ฒญ์ ๋ณธ๋ฌธ์ด ํ ์คํธ, ๋ค์ด์ค๋ ๋ฐ์ดํฐ๋ ํ ์คํธ๋ผ์ ๋ฌธ์์ด๋ก ์ ํํ ์ ์๋ ๊ฒ์ด๋ค.
const parsedBoddy = Buffer.concat(body).toString();
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
//createServer : ๋ค์ด์ค๋ ๋ชจ๋ ์์ฒญ์ ์คํ๋ ํจ์๋ฅผ ์ ์
const url = req.url;
const method = req.method;
if (url === '/') { //url์ด ์๋ฌด๋ฐ ๊ฒฝ๋ก๊ฐ ์๋ค๋ฉด,
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write(
'<body><form action="/message" method="POST"><input type="text" name="message"><button = "submit">Send</button></form></body>');
res.write('</html>');
return res.end();
}
if (url === '/message' && method === 'POST') {
//์์ฒญ ๋ณธ๋ฌธ์ ์ง์ ๊ฒ์ด๋ฏ๋ก ์์๋ช
body๋ก ์์ฑ ํ, ๋น ๋ฐฐ์ด๋ก ๋ง๋ค์ด ์ฃผ์.
const body = [];
//post ๋ฉ์์ง๋ฅผ ๋ฐ์ ๋ ์๋ต์ ๋ณด๋ด๊ฑฐ๋ ํ์ผ์ ์ฐ๊ธฐ ์ ์ ์์ฒญ ๋ฐ์ดํฐ ๋ฐ์ผ๋ ค๋ฉด
//req.๋ฅผ ์
๋ ฅํด์ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ๋ฑ๋กํ๋ฉด ๋๋ค.
//createServer๊ฐ ๋์ ์๋ฒ๋ฅผ ์์ฑํด์ฃผ๋ ๋ฐ๋ฉด, on ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ง์ ํ ์ ์๋ค.
req.on('data', (chunk) => {
console.log(chunk);
body.push(chunk); //์ด์ ์์ฒญ์ ๋ํ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ป์ ๋๊น์ง ํจ์๊ฐ ์คํ๋ ๊ฒ
});
req.on('end', () => {
const parsedBoddy = Buffer.concat(body).toString(); //๋ฒํผ๊ฐ ์๊ธด ์ฒญํฌ์ ์์
console.log(parsedBoddy);
// ์ฒญํฌ๋ค์ ๋ฐ์ ๋ค์์ ์ฒญํฌ๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํด์๋ ๋ฒํผ(๋ฒ์ค์ ๋ฅ์ฅ) ๊ฐ์ ์ฅ์์์ ์ํธ์์ฉ ํ ์ ์์ ๊ฒ์ด๋ค.
});
//on ๋ฉ์๋๋ ํน์ ์ด๋ฒคํธ๋ฅผ ๋ค์ ์ ์๋๋ก ํ๋ค.
//์ฒซ๋ฒ์งธ ์ธ์ : data์ด๋ฒคํธ๋ ์ ์ฒญํฌ๊ฐ ์ฝํ ์ค๋น๊ฐ ๋ ๋๋ง๋ค ๋ฐ์ดํฐ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ ๋ฐ์ ๋ฒํผ๊ฐ ๋์์ ์ค๋ค.
//๋๋ฒ์งธ ์ธ์ : ๋ชจ๋ ๋ฐ์ดํฐ ์ด๋ฒคํธ์ ์คํ๋ ํจ์๋ฅผ ๋ฃ์ด์ผ ํ๋ค.
// ์ฌ๊ธฐ์๋ ๋ค์ด์ค๋ ๋ชจ๋ ๋ฐ์ดํฐ์ ์คํ๋ ํจ์๋ฅผ ์ ์ํ๋ค.
fs.writeFile('message.txt', '๋๋ฏธํ
์คํธ');
//res.writeHead(302, {});
res.statusCode = 302;
res.setHeader('Location', '/')//๋ธ๋ผ์ฐ์ ๊ฐ ์๋ฝํ๋ ๋ํดํธ ํค๋,
//์์น์ ๊ฒฝ์ฐ /๋ก ์ค์ ํ๊ฒ ๋๋ฉด ๋ด๊ฐ ์ด๋ฏธ ์คํ ์ค์ธ ํธ์คํธ๋ฅผ ์๋์ผ๋ก ์ฌ์ฉํ๊ฒ ๋๋ค.
return res.end();
}
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write('<body><h1>Hello from my Node.js Server!</h1></body>');
res.write('</html>');
res.end('Hello Node'); //์ด ์์ ๋ถํฐ๋ ๋ ์ด์ ์๋ฌด ๊ฒ๋ ์
๋ ฅํด์๋ ์๋จ
//์ด ๋ถ๋ถ์ ์ฐ๋ฆฌ๊ฐ ํด๋ผ์ด์ธํธ๋ก ๋ค์ ์ ์กํ๋ ๋ถ๋ถ์ผ๋ก, Node.js๊ฐ ํด๋ผ์ด์ธํธ๋ก ๋ค์ ์ ์กํ๊ธฐ ๋๋ฌธ
});
server.listen(3001);
์์ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ๊ณ , ์๋ฒ๋ฅผ ์ฌ์คํ์์ผ๋ณด์.
๋ก๊ทธ๋ฅผ ๋ณด๋ฉด ๋๊ฐ์ ์์๊ฐ ์๋ค.
์ฒซ๋ฒ์งธ๋ ๊ฐ๋ฐ์๊ฐ ์ฒ๋ฆฌํ ์ ์๋ ์ฒญํฌ์ด๋ค <Buffer ..~~~> conosole.log(chunk)
๋ฐ๋ฉด, parsedBody๋ ์ ์ ๊ฐ ์ ๋ ฅํ ๋ฉ์์ง๋ฅผ ๋ฐ์์ ๊ฐ๋ฐ์๊ฐ ์ฒ๋ฆฌํ ์ ์๋ ๋ฉ์์ง๋ฅผ ์ฐ์ถํ๋ค.
(input์ name์ message ๋ก ์ง์ด์ message๋ก ์์ํจ)
์ฌ๊ธฐ form์ด ๋ชจ๋ ์ ๋ ฅ ๋ฐ์ดํฐ์ ํจ๊ป ์๋์ผ๋ก ์์ฒญ์ ๋ณด๋ด ์์ฒญ ๊ฐ์ฒด์ ํค-๊ฐ ์์ผ๋ก ๋ฃ๋๋ค.
<form action="/message" method="POST">
์ฌ์ฉ์๊ฐ ์ ๋ ฅํ๋ ๊ฒ์ด ๊ฐ์ด๋ฉฐ, ์ ๋ ฅ์ ์ฃผ์ด์ง๋ ์ด๋ฆ์ด ํค์ด๋ค.
<input type="text" name="message">
๋ก๊ทธ๋ฅผ ๋ณด๋ฉด ํค์ ๊ฐ์ ๋ฑํธ๋ก ๋๋์ด์ ธ ์๋ค. ๊ทธ๋ผ ๋น๋ก์ ํ์ผ์ ์ ๋ ฅ์ ์ ์ฅํ ์ ์๊ฒ ๋๋ค.
๊ทธ๋ผ ๋ฐ๋ก req.on(end)์ ์๋ก์ด ์์ message๋ฅผ ๋ง๋ค๊ณ paresdBody.split์ ๋ฑํธ๋ฅผ ๋ฃ์ ํ ๊ฒฐ๊ณผ ๋ฐฐ์ด์ ๋๋ฒ์งธ ์์์ธ ์ธ๋ฑ์ค 1์ ์์๋ก ๋๋ ์๊ฐ์ ํ ์๊ฐ ์๋ค.
req.on('end', () => {
const parsedBody = Buffer.concat(body).toString(); //๋ฒํผ๊ฐ ์๊ธด ์ฒญํฌ์ ์์
console.log(parsedBody);
const message = parsedBody.split('=')[1];
console.log(message);
// ์ฒญํฌ๋ค์ ๋ฐ์ ๋ค์์ ์ฒญํฌ๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํด์๋ ๋ฒํผ(๋ฒ์ค์ ๋ฅ์ฅ) ๊ฐ์ ์ฅ์์์ ์ํธ์์ฉ ํ ์ ์์ ๊ฒ์ด๋ค.
});
์ด์ writeFileSync๋ฅผ ํจ์์ ๋ง์ง๋ง์ ๋ง๋ถ์ธ๋ค. ๋๋ฒ์งธ ์ธ์๋ก๋ message๋ฅผ ๋ฃ์ด์ค๋ค.
req.on('end', () => {
const parsedBody = Buffer.concat(body).toString(); //๋ฒํผ๊ฐ ์๊ธด ์ฒญํฌ์ ์์
console.log(parsedBody);
const message = parsedBody.split('=')[1];
console.log(message);
fs.writeFileSync('message.txt', message);
// ์ฒญํฌ๋ค์ ๋ฐ์ ๋ค์์ ์ฒญํฌ๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํด์๋ ๋ฒํผ(๋ฒ์ค์ ๋ฅ์ฅ) ๊ฐ์ ์ฅ์์์ ์ํธ์์ฉ ํ ์ ์์ ๊ฒ์ด๋ค.
});
์คํธ ๋ค์ ์๋ฒ๋ฅผ ์ฌ์์ํ๊ณ
LESSERAFIM์ ์ ๋ ฅํ๋ ๋ค์๊ณผ ๊ฐ์ด ์ ์ ์ ์ ๋ ฅ๊ฐ์ด ์๋ก์ด ํ์ผ ์์ฑ ํ ์ ์ฅ๊น์ง ์ ๋๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค!
์ ์ฒด ์ฝ๋
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
//createServer : ๋ค์ด์ค๋ ๋ชจ๋ ์์ฒญ์ ์คํ๋ ํจ์๋ฅผ ์ ์
const url = req.url;
const method = req.method;
if (url === '/') { //url์ด ์๋ฌด๋ฐ ๊ฒฝ๋ก๊ฐ ์๋ค๋ฉด,
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write(
'<body><form action="/message" method="POST"><input type="text" name="message"><button = "submit">Send</button></form></body>');
res.write('</html>');
return res.end();
}
if (url === '/message' && method === 'POST') {
//์์ฒญ ๋ณธ๋ฌธ์ ์ง์ ๊ฒ์ด๋ฏ๋ก ์์๋ช
body๋ก ์์ฑ ํ, ๋น ๋ฐฐ์ด๋ก ๋ง๋ค์ด ์ฃผ์.
const body = [];
//post ๋ฉ์์ง๋ฅผ ๋ฐ์ ๋ ์๋ต์ ๋ณด๋ด๊ฑฐ๋ ํ์ผ์ ์ฐ๊ธฐ ์ ์ ์์ฒญ ๋ฐ์ดํฐ ๋ฐ์ผ๋ ค๋ฉด
//req.๋ฅผ ์
๋ ฅํด์ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ๋ฑ๋กํ๋ฉด ๋๋ค.
//createServer๊ฐ ๋์ ์๋ฒ๋ฅผ ์์ฑํด์ฃผ๋ ๋ฐ๋ฉด, on ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ง์ ํ ์ ์๋ค.
req.on('data', (chunk) => {
console.log(chunk);
body.push(chunk); //์ด์ ์์ฒญ์ ๋ํ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ป์ ๋๊น์ง ํจ์๊ฐ ์คํ๋ ๊ฒ
});
req.on('end', () => {
const parsedBody = Buffer.concat(body).toString(); //๋ฒํผ๊ฐ ์๊ธด ์ฒญํฌ์ ์์
console.log(parsedBody);
const message = parsedBody.split('=')[1];
console.log(message);
fs.writeFile('message.txt', message);
// ์ฒญํฌ๋ค์ ๋ฐ์ ๋ค์์ ์ฒญํฌ๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํด์๋ ๋ฒํผ(๋ฒ์ค์ ๋ฅ์ฅ) ๊ฐ์ ์ฅ์์์ ์ํธ์์ฉ ํ ์ ์์ ๊ฒ์ด๋ค.
});
//on ๋ฉ์๋๋ ํน์ ์ด๋ฒคํธ๋ฅผ ๋ค์ ์ ์๋๋ก ํ๋ค.
//์ฒซ๋ฒ์งธ ์ธ์ : data์ด๋ฒคํธ๋ ์ ์ฒญํฌ๊ฐ ์ฝํ ์ค๋น๊ฐ ๋ ๋๋ง๋ค ๋ฐ์ดํฐ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ ๋ฐ์ ๋ฒํผ๊ฐ ๋์์ ์ค๋ค.
//๋๋ฒ์งธ ์ธ์ : ๋ชจ๋ ๋ฐ์ดํฐ ์ด๋ฒคํธ์ ์คํ๋ ํจ์๋ฅผ ๋ฃ์ด์ผ ํ๋ค.
// ์ฌ๊ธฐ์๋ ๋ค์ด์ค๋ ๋ชจ๋ ๋ฐ์ดํฐ์ ์คํ๋ ํจ์๋ฅผ ์ ์ํ๋ค.
//fs.writeFile('message.txt', '๋๋ฏธํ
์คํธ');
//res.writeHead(302, {});
res.statusCode = 302;
res.setHeader('Location', '/')//๋ธ๋ผ์ฐ์ ๊ฐ ์๋ฝํ๋ ๋ํดํธ ํค๋,
//์์น์ ๊ฒฝ์ฐ /๋ก ์ค์ ํ๊ฒ ๋๋ฉด ๋ด๊ฐ ์ด๋ฏธ ์คํ ์ค์ธ ํธ์คํธ๋ฅผ ์๋์ผ๋ก ์ฌ์ฉํ๊ฒ ๋๋ค.
return res.end();
}
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write('<body><h1>Hello from my Node.js Server!</h1></body>');
res.write('</html>');
res.end('Hello Node'); //์ด ์์ ๋ถํฐ๋ ๋ ์ด์ ์๋ฌด ๊ฒ๋ ์
๋ ฅํด์๋ ์๋จ
//์ด ๋ถ๋ถ์ ์ฐ๋ฆฌ๊ฐ ํด๋ผ์ด์ธํธ๋ก ๋ค์ ์ ์กํ๋ ๋ถ๋ถ์ผ๋ก, Node.js๊ฐ ํด๋ผ์ด์ธํธ๋ก ๋ค์ ์ ์กํ๊ธฐ ๋๋ฌธ
});
server.listen(3005);
์ง๊ธ๊น์ง๋ Node.js์ ๋ฏธ๊ฐ๊ณต ๋ ผ๋ฆฌ๋ฅผ ์ดํด๋ณด์๊ณ
๋์ค์๋ ์ด๋ฌํ ๋ฏธ๊ฐ๊ณต ๋ ผ๋ฆฌ๋ฅผ ์จ๊ธฐ๋ Express.js๋ฅผ ์ฌ์ฉํ๋ค.
๊ทธ๋ฌ๋ ์ง๊ธ๊น์ง์ ํ์ต์ผ๋ก
Express.js ๊ฐ์ ํด์ด ์ด๋ป๊ฒ ๊ณผ์ ์ ๋จ์ํํ๋์ง ์ ์ ์๋ค.
'Javascript > Node.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Postman] Rest APIs ๊ฐ๋ฐ์ ์ํ ํฌ์คํธ๋งจ ์ ์ (0) | 2022.12.12 |
---|---|
[Node.js] (6) Single Thread, Event Loop & Blocking Code (0) | 2022.12.09 |
[Node.js] (4) ์๋ต ์ ์ก (0) | 2022.11.24 |
[Node.js] (3) ์์ฒญ ๊ฐ์ฒด (0) | 2022.11.23 |
[Node.js] (2) Node์ ๋ผ์ดํ์ฌ์ดํด ๋ฐ ์ด๋ฒคํธ ๋ฃจํ (0) | 2022.11.23 |